Skip to content
loading...

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

fleetfootmike profile image Mike Whitaker github logo ・1 min read

Shell options you didn't know you needed (5 Part Series)

1) Shell command options you didn't know you needed #1 2) Shell command options you didn't know you needed #2 3) Shell command options you didn't know you needed #3 4) Shell command options you didn't know you needed (or possibly even existed) #4 5) 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

twitter logo DISCUSS
Discussion
markdown guide
Classic DEV Post from Feb 23

How The DEV Community transformed my Career ?

A Problem with Developers ! Developers are intelligent people who love to write code in...

Mike Whitaker profile image
Perl dev and evangelist, tabletop gamer, photographer, musician, writer.