Useful OneLiners
# 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 ...
Perl “One-Liners”
Perl “One Liners” are -surprise surprise- little perl programs written on one line.
Here are a few simple examples :
Guess what this one does
$ perl -e 'print "Hello";' -e 'print " World\n"'
DOS to Unix text convert
$ perl -i -pe 's/\r//g' file
Unix to DOS text convert
$ perl -i -pe 's/\n/\r\n/' file
‘Fun’ with columns:
$ echo a b c | perl -lane 'print $F[1]'
b
$ echo a b c | perl -lane 'print "@F[0..1]"'
a b
$ echo a b c | perl -lane 'print "@F[-2,-1]"'
b c
More one-liners will follow…