DEV Community

Mike Whitaker
Mike Whitaker

Posted on

Shell command options you didn't know you needed #5

Technically, this one's a Perl option, but... if your Perl usage is at the sysadmin/devops level (i.e. the Swiss Army Chainsaw) you might find this very handy for processing files and running potentially complex Perl code against every line in the file. Particularly if, like me, you speak better Perl than AWK :D

perl -ne (and perl -pe)

The -ne option wraps the code you provide in the argument with a loop over every line in all the input files, with the line (with trailing CR) stored in $_. The -pe option does the same, but adds an implicit print $_ at the end of the loop.

A couple of trivial examples:

Extract unique IP addresses from your Apache log.

perl -pe 's/^(\d+.\d+.\d+.\d+).*/$1/;' */access.log | sort -u

Poor man's grep with Perl regexes.

perl -ne 'print if /admin\.php/;' access.log

Top comments (0)