Low Orbit Flux Logo 2 D

Perl One-Liners

A Perl One-Liner is a Perl script that is run as a single command passed to the interpreter all on one line. It isn’t saved to a file. All you really need to create the most basic one liner is the “-e” flag. If you want a script that behaves similar to AWK, Sed, or grep, then you will want an autmatically created loop. To do this you will generally either use the “-pe” or “-ne” parameters depending on whether or not you want it to print each line of input or not. The “-i” flag is optional but will allow you to edit files in place which is pretty handy.

-p create printing loop around commands
-n create non-printing loop around commands
-e specify script as argument instead of file
-i modify input file in place, also creates a backup
-w enable warnings
-d run under debugger
-t tainted code will be treated as warning

Replace the first occurence of “user1” with “user2” for each line:

ps -ef | perl -pe 's/user1/user2/'

Globally replace all numbers with the letter x:

ps -ef | perl -pe 's/\d/x/g'

Print columns 0 and 5:

ps -ef | perl -ne '@x=split(/ /, $_); print( @x[0]." ".@x[5]."\n")'