perl-one-linersPerl One-liners is a beautifully written manual, if I can call it like that, that empowers sysadmins with a Swiss Army knife to get dirty jobs done. The author, Peteris Krumins, starts with simple one-liners that cater for common tasks like searching, replacing or identifying blocks of texts. He goes ahead mixing various components of regular expression to tackle more complex situations. With each line of code Peteris elaborates on the options used.

By the end of the book one should master enough of Perl code to write beautiful scripts.

Let’s have a look at this extract …

iptables -L -nvx | perl -lane '$pkts += $F[0]; END { print $pkts }'

The iptables program outputs the packets in the first column. All you have to do to find out how many packets have passed through the firewall rules is sum the numbers in the first column. Although iptables will output table headers as well, you can safely ignore these because Perl converts them to zero for the += operation. How about getting a list of all users on the system?