December 15, 2013, 5:32 PM — Perl One-Liners: 130 Programs that Get Things Done by Peteris Krumins, no starch press, 2014
When I first saw the title of this book, I asked myself "An entire book of one-liners? Is that even possible?". But, yes, it's not only possible; it's actually a good idea. Unlike most books that aim to teach you about programming/scripting by starting with the basics and then slowly adding detail until you're building complicated programs, this one starts with crystallizations of cleverness -- precise, single purpose, no wasted characters code -- and then helps you understand what each one-liner does for you and how/why it works. This is an interesting, effective approach and one that is likely to provide you not only with immediately useful time-saving Perl commands, but also with some long lasting insights into how you can make use of Perl's many features.
What is a one-liner and why are one-liners such a big deal?
Some people describe one-liners as a "dirty little secret". That special thrill that some of us get when we can do in one line of text what many others require numerous lines in a script to accomplish is one of the pinnacles of Unix vanity. A one-liner is slick and demonstrates skill with the language in which it is crafted. But it also can serve as the right tool for the job when you want to get a lot of work done without a lot of effort. You type a single line of text on the command line and maybe avoid an hour of potentially boring work or half an hour of trying to do the same thing with a simple script. A one-liner that I've probably used hundreds of times or more is one that I generally refer to as the "perl pie" command. It allows me to make changes to a file without opening it or having to make use of a temporary file. It's extremely useful on the command line or as line in a script that needs to change the content of a file along with many other things. For those of you unfamiliar with perl "pie" commands, they look like this:
$ perl -p -i -e 's/this/that/g' myfile
The "-p -i -e" part of this command could also be expressed as "-pi -e" and this is, in fact, like the example that the author uses to start his journey through 130 really useful Perl one-liners. He shows us the command shown below and asks us to imagine that we have to make repeated word substitutions in a file and to compare this one liner with the process of editing the file and making the changes in a more manual way.
perl -pi -e s/you/me/g' file
The -p -i and -e options don't have to be provided in the order shown, but I find them easier to remember that way, especially for anyone who likes pie as much as I do. ;-) These three options:
-p place a printing loop around your command so that it acts on each line of input -i modify command input in-place -e provide the program as an argument (i.e., on the command line)
A similar Perl pie command might change Windows (DOS) style line endings (carriage return, linefeed) to Unix style line endings (linefeed only) or to replace other bytes in a file whether the file is a text file or a binary file. This particular command strips out ^M (carriage return) characters by changing carriage return linefeed sequences to simple linefeeds. This has the same effect as the dos2unix command.
$ perl -p -i -e 's/\015\012/\n/' story
While I use Perl pie commands fairly often, before reading this book, I really had no idea how many smart things I could do with Perl one-liners. Here's one from page 11 that double spaces lines except for those lines that are already blank.
perl -pe '$_ .= "\n" unless /^$/'
If you want to read out this line of command text, think :whatever your line of input ($_) contains, we will add (.=) a newline (\n) unless the line starts (^) and ends ($) without any additional content (nothing between the ^ and the $). So, even if it appears a bit cryptic when you first look at it, commands like this can quickly become familiar.
And, as the author points out, perl one-liners like this do one thing really well. What I never grasped before is how many tasks one-liners can accomplish and how much I can learn about Perl by studying some that are especially well crafted. Here's one from page 80 of the book that prints only lines that contain nothing but alphabetic characters:
perl -ne 'print if /^[[:alpha:]]+$/
In this one, we see the :alpha: string that matches alphabetic characters, the + that says any number of them and the ^ and $ that mark the beginning and end of the line. So, the effect is "print if the line contains nothing but alphabetic characters. That means no blanks, no digits, etc. Just letters.
The book provides one-liners for lots of things you might want to do and, the great part is that you don't have to figure them all out yourself. It is organized in useful sections so that you can easily find what you're looking for. Chapters 2-7 cover:
spacing -- one-liners for double spacing, removing blank lines, etc.
numbering -- one-liners for numbering lines and words
calculations -- one-liners for finding minimum and maximizing values, calculating dates, etc.
working with arrays and strings -- one-liners for generating passwords, creating strings
text conversion and substitution -- one-liners for changing, converting and substituting text
selectively printing and deleting lines -- one-liners for printing lines that are repeated or are the shortest lines in files
Chapter 8 covers useful regular expressions and how to use them in one-liners.
The book also has some very useful appendices. The first covers Perl's special variables. If, for example, you don't know what $\ represents, you can look it up and have yourself an "aha!" moment. This appendix can help you understand many of the one-liners in the earlier chapters. The next covers using one-liners on windows -- installing Perl, using bash on Windows, using Perl in a bash window and using Perl one-liners in a command prompt or in PowerShell. The last, entitled "perl1line.txt" is an appendix that lists all of the one liners from earlier chapters with just the briefest descriptions. It basically serves as a one-liner cheat sheet.
I like this book. It lends itself to helping find what you need or find something very close to what you need and tweaking it until it's just right. The take home for me is that one-liners are not just useful in themselves; they also can help you understand the finer nuances of how Perl works and what it can do for you. This is NOT a read front-to-back kind of book, though you could use it that way, but a reference that can jump start so many tasks that you might want to address with perl. A great reference for when you want to do something with Perl without spending an hour or two trying to figure out how.
Read more of Sandra Henry-Stocker's Unix as a Second Language blog and follow the latest IT news at ITworld, Twitter and Facebook.