Celebrazio Net



Contact Us

Perl One-Liners

Jun., 2019

Quick Perl. No time to explain. Just try them.

 
perl -e 'use URI::Escape;
   $safe= uri_escape("http://www.celebrazio.net/contact.html");
   print $safe . "\n";'

perl -e 'print @INC:'  ## gives my included libraries 

perl -e 'use Data::Dumper; print Dumper(\%INC);'
        ## lots of other cool stuff too

## install pmtools and then
pminst   ## gives list of all installed perl modules

cpan -l  ## another (better?) way to list installed modules

## keep CPAN modules up-to-date: 
perl -MCPAN -e "install Bundle::CPAN"

perl -e 'print join("\n",glob("\/dir{1,3,5,7,9}\/filename{v1,v2}.html")) . "\n"'

perl -p -i.bak -e 's/foo/bar/g' original.txt  ## creates .bak as backup

## grep from file1 -> list to stdout
 perl -ne ' if (@m = m[(url_.{4,9}_string)]gs ){foreach my $a (@m) { print "$a\n"} ;}'
   file1.txt

## random dice rolls
perl -e ' for ($i=0;$i<50;$i++) { print int(rand 6) +1 }; print "\n"'

## compare checksums
cksum file1 file2 | perl -ane '$x ||= $F[0]; warn if $x != $F[0]'

## remove whitespace at end of line, but keep newline: 
perl -lpe 's/\s*$//'

## nice range operator to print whole lines between start and end marker lines:  
perl -ne 'print if /-----BEGIN PGP PUBLIC KEY BLOCK-----/../-----END PGP PUBLIC KEY BLOCK-----/' FILE    

# print utf8
perl -e ' use Encode qw/decode/; $terms=`cat file.txt`; print decode("utf8", $terms) . "\n";'

"With a PC, I always felt limited by the software available. On Unix, I am limited only by my knowledge."
-- Peter J. Schoenster





1998-2024 Celebrazio.net