# Deparse something perl -MO=Deparse -e '' # wc -l # Clarified as per node 232599 perl -le 'print $==()=<>' file # Time to the 1000000000 rollover perl -le 'print reverse(1..9)-time' perl -le 'print 1e9-time' # Count the number of lines in a file perl -lpe'}{$_=$.' /etc/passwd # Sum the numbers in the first column of a file perl -lpe '$,+=$_}{$_=+$,' file perl -lpe '${}}+=+${_}}{${_}+=+${}}' numfile # Remove the first column perl -nale 'shift@F;print"@F"' # Get config info perl -MConfig -le 'print Config::myconfig()' perl -MConfig -le 'print $Config{startperl}' perl -MConfig=myconfig -le 'print myconfig()' perl -V:startperl # Find the version number of a module perl -le 'eval "require $ARGV[0]" and print $ARGV[0]->VERSION' Some::Module # Check if a module exists perl -le 'print 0 + eval "require $ARGV[0]" ' Some::Module # dos2unix perl -i -pe 's/\r//' dos.txt # JAAH perl -le 'use Inline AWK=>q+function j(){print"Just Another Awk Hacker"}+;j' # wc -l perl -MInline=awk,END{NR} -le 'print awk()' file # Create a slice list perl -le '@a = (0,1,2,3); @b = map {$_,$_+@a} @a; print "@b"' perl -le '@a = (0,1,2,3); @b = map {$_,$_+@a} (0..$#a); print "@b"' # Weave 2 arrays perl -le '@a=(1,2);@b=('a','b'); @c = (@a,@b)[map {$_,$_+@a} (0..$#a)]; print "@c"' # Doublespace a file perl -pe '$_.=$/' file perl -00lpe 's/\n/\n\n/g' file perl -pe '$_=$/.$_ if /\S/' file.txt | cat -e # Print every line twice perl -pe '$\=$_' file # Ghost in the machine. perl -MCPAN -e 'install jmcnamara & _ x ord $ ;' | tail -1 # Last line of a file perl -pe '$*=$_}{$_=$*' file perl -e 'print+()[-1]' file # tail perl -ne '$i=$.%6; $a[$i]=$_; END{print @a[$i+1..$#a,0..$i]}' file perl -ne 'END{print@a[$i+1..$#a,0..$i]}$a[$i=$.%5]=$_' file perl -ne 'END{print@a[$i+1..@a,0..$i]}$a[$i=$.%5]=$_' file perl -pe '$a[$i=$.%5]=$_}{print@a[$i+1..@a,0..$i]' file perl -pe '$_[$==$.%5]=$_}{print@_[$=+1..@_,0..$=]' file # -s perl -sle 'print $#' -- -#=3 # Print filename before line perl -pe '$_="$ARGV: $_"' file # Print filename and line number before line perl -ne 'print $ARGV,":", $., ": ", $_; $. = 0 if eof' file file2 # extract pod perl -nle 'print if /^=/ .. /^=cut/' file # extract code perl -nle 'print if not /^=/ .. /^=cut/' file # print print japh perl -e '$#="Just Another Perl Hacker$/"; print print' # print a line of a file perl -ne 'print and exit if $. == 3' file perl -ne 'print and last if $. == 3' file # grep -v perl -ne 'print if not /foo/' file perl -pe 'goto LINE if /foo/' file # Generate a random word perl -le 'print map{(a..z)[rand 26]} 0..rand 10' # print lines with only one word perl -nae 'print if @F == 1' file # Remove repeated lines perl -i.bak -ne 'print unless $h{$_}++' file sort -o thefile -u file # merlyn # remove all occurance of pattern except for the first one perl -ne 'print unless /pattern/ and $i++' file # Generate a list of months cal 2002 | perl -lane '@F==3&&print for@F' cal 2002 | perl -lane '@F==3&&print substr $_, 0, 3 for @F' # remove blank lines perl -ane '@F&&print' file perl -lp00ee file # abigail perl -llpp00ee file # abigail perl -l -00 -perl file # merlyn # BCD encode perl -le 'print join " ", map {ord pack "H2", $_} shift =~ /../g' num # BCD decode perl -le 'print unpack "H*", pack "C*", @ARGV' num1 num2 ... perl -le 'print map {int($_/16).($_%16)} @ARGV' num1 num2 ... # Add newline to the end of a file perl -plee file1 file2 ... perl -pe '$_ .= $/ if eof and not /\n/' file1 file2 ... perl -i -pl -end file # Check a file for tabs in a file perl -ne 'die "File contains tabs.\n" if /\t/' file perl -ne 'die "File $ARGV contains tabs\n" if /\t/' file # Print a match and exit perl -ne 'print and last if /foo/' file # Exit after first match perl -ne 'print; last if /foo/' file # Exit before first match perl -pe 'last if /foo/' file perl -pe 'die $_ if /foo/' file # Count the vowels in a file perl -lne 'END{print $c} $c += tr/aeiou//' file