This is the introduction to perl one-liners. Originally I wrote this introduction for my third e-book, however later I decided to make it a part of the free e-book preview and republish it here as this article. Introduction to Perl one-linersPerl one-liners are small and awesome Perl programs that fit in a single line of code and they do one thing really well. These things include changing line spacing, numbering lines, doing calculations, converting and substituting text, deleting and printing certain lines, parsing logs, editing files in-place, doing statistics, carrying out system administration tasks, updating a bunch of files at once, and many more. Perl one-liners will make you the shell warrior. Anything that took you minutes to solve, will now take you seconds! Let's look at several examples to get more familiar with one-liners. Here is one: perl -pi -e 's/you/me/g' file This one-liner replaces all occurrences of the text The How about doing the same replacement in multiple files? Just specify them on the command line! perl -pi -e 's/you/me/g' file1 file2 file3 Now let's do the same replacement only on lines that match perl -pi -e 's/you/me/g if /we/' file Here we use the conditional perl -pi -e 's/you/me/g if /\d/' file Now how about finding all repeated lines in a file? perl -ne 'print if $a{$_}++' file This one-liner records the lines seen so far in the How about numbering lines? Super simple! Perl has the perl -ne 'print "$. $_"' You can also achieve the same by using perl -pe '$_ = "$. $_"' Here each line gets replaced by the string How about we combine the previous two one-liners and create one that numbers repeated lines? Here we go: perl -ne 'print "$. $_" if $a{$_}++' Now let's do something different. Let's sum up all the numbers on each line. We'll use the perl -MList::Util=sum -alne 'print sum @F' The How about finding the date 1299 days ago? That's also solvable by a simple one-liner: perl -MPOSIX -le ' @now = localtime; $now[3] -= 1299; print scalar localtime mktime @now ' This one-liner didn't quite fit in one line, but that's just because my blog has narrow space for content. What happens here is we modify the 4th element of the structure returned by How about generating an 8 letter password? Again, solvable elegantly with a one-liner: perl -le 'print map { (a..z)[rand 26] } 1..8' The Here is another one. Suppose you want to quickly find the decimal number that corresponds to an IP address. You can use the perl -le 'print unpack("N", 127.0.0.1)' This one-liner uses a vstring, which is a version literal. The IP address Btw, I once created a cheat sheet with all the Now how about calculations? Let's find the sum of the numbers in the first column: perl -lane '$sum += $F[0]; END { print $sum }' Here the lines automatically get split up into fields through the Now let's find out how many packets have gone through all the iptables -L -nvx | perl -lane ' $_ = $F[0]; $pkts += $_ if /^\d/; END { print $pkts } ' The iptables program outputs the packets as the first field. All we do is check if the first field is numeric (because it also outputs labels header), and if so, sum the packets up on the first column, just like in the previous one-liner. How about getting a list of the names of all users on the system? perl -a -F: -lne 'print $F[4]' /etc/passwd Combining As you can see, knowing Perl one-liners lets you accomplish many tasks quickly. If you wish to learn more and become really fast in the shell, I suggest you get a copy of my "Perl One-Liners Explained" e-book. The e-book contains 130 unique one-liners and many of them are presented in several different ways, so the total number of one-liners in this book is over 200. If you enjoy my writing, you can subscribe to my blog, follow me on Twitter or Google+. |
|