Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Softpanorama Search

Perl One Liners

Old News
;-)

See also

Recommended Books Recommended Links

Perlrun man page

Debugging
           

Perl one liners are important part of any Perl programmer arsenal as well as any system administrator arsenal.

Matz Kindahl - Collection of Perl programs

These are one liners that might be of use. Some of them are from the net and some are one that I have had to use for some simple task. If Perl 5 is required, the perl5 is used.
perl -ne '$n += $_; print $n if eof'
perl5 -ne '$n += $_; END { print "$n\n" }'
To sum numbers on a stream, where each number appears on a line by itself. That kind of output is what you get from cut(1), if you cut out a numerical field from an output. There is also a C program called sigma that does this faster.

 

perl5 -pe 's/(\w)(.*)$/\U$1\L$2/'
perl5 -pe 's/\w.+/\u\L$&/'
To capitalize the first letter on the line and convert the other letters to small case. The last one is much nicer, and also faster.

 

perl -e 'dbmopen(%H,".vacation",0666);printf("%-50s: %s\n",$K,scalar(localtime(unpack("L",$V)))while($K,$V)=each(%H)'
Well, it is a one-liner. :)
You can use it to examine who wrote you a letter while you were on vacation. It examines the file that vacation(1) produces.

 

perl5 -p000e 'tr/ \t\n\r/ /;s/(.{50,72})\s/$1\n/g;$_.="\n"x2'
This piece will read paragraphs from the standard input and reformat them in such a manner that every line is between 50 and 72 characters wide. It will only break a line at a whitespace and not in the middle of a word.

 

perl5 -pe 's#\w+#ucfirst lc reverse $&#eg'
This piece will read lines from the standard input and transform them into the Zafir language used by Zafirs troops, i.e. "Long Live Zafir!" becomes "Gnol Evil Rifaz!" (for some reason they always talk using capital letters).
Andrew Johnson and I posted slightly different versions, and we both split the string unnecessarily. This one avoids splitting the string.

 

perl -pe '$_ = " $_ "; tr/ \t/ /s; $_ = substr($_,1,-1)'
This piece will remove spaces at the beginning and end of a line and squeeze all other sequences of spaces into one single space.
This was one of the "challenges" from comp.lang.perl.misc that occurs frequently; I am just unable to resist those. :)

Tom Christiansen  once posted a list of one line perl programs to do many common  command-line tasks.

   It included:
   # run contents of "my_file" as a program
   perl my_file

   # run debugger "stand-alone"
   perl -d -e 42

   # run program, but with warnings
   perl -w my_file

   # run program under debugger
   perl -d my_file

   # just check syntax, with warnings
   perl -wc my_file

   # useful at end of "find foo -print"
   perl -nle unlink

   # simplest one-liner program
   perl -e 'print "hello world!\n"'

   # add first and penultimate columns
   perl -lane 'print $F[0] + $F[-2]'

   # just lines 15 to 17
   perl -ne 'print if 15 .. 17' *.pod

   # in-place edit of *.c files changing all foo to bar
   perl -p -i.bak -e 's/\bfoo\b/bar/g' *.c

   # command-line that prints the first 50 lines (cheaply)
   perl -pe 'exit if $. > 50' f1 f2 f3 ...

   # delete first 10 lines
   perl -i.old -ne 'print unless 1 .. 10' foo.txt

   # change all the isolated oldvar occurrences to newvar
   perl -i.old -pe 's{\boldvar\b}{newvar}g' *.[chy]

   # command-line that reverses the whole file by lines
   perl -e 'print reverse <>' file1 file2 file3 ....

   # find palindromes
   perl -lne 'print if $_ eq reverse' /usr/dict/words

   # command-line that reverse all the bytes in a file
   perl -0777e 'print scalar reverse <>' f1 f2 f3 ...

   # command-line that reverses the whole file by paragraphs
   perl -00 -e 'print reverse <>' file1 file2 file3 ....

   # increment all numbers found in these files
   perl i.tiny -pe 's/(\d+)/ 1 + $1 /ge' file1 file2 ....

   # command-line that shows each line with its characters backwards
   perl -nle 'print scalar reverse $_' file1 file2 file3 ....

   # delete all but lines beween START and END
   perl -i.old -ne 'print unless /^START$/ .. /^END$/' foo.txt

   # binary edit (careful!)
   perl -i.bak -pe 's/Mozilla/Slopoke/g' /usr/local/bin/netscape

   # look for dup words
   perl -0777 -ne 'print "$.: doubled $_\n" while /\b(\w+)\b\s+\b\1\b/gi'

   # command-line that prints the last 50 lines (expensively)
   perl -e 'lines = <>; print @@lines[ $#lines .. $#lines-50' f1 f2 f3 ...



Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Disclaimer:

Last updated: August 15, 2009