Mike Whitaker
Posted on March 17, 2020
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
Posted on March 17, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.