Buy from No Starch Press
Use discount code CATONMAT for 30% off!

Also available at:
Amazon Kindle
Barnes and Noble

I've fantastic news - I've become a published author! No Starch Press just published my book Perl One-Liners!

Some programs are powerful because of their thousands of lines of code, and some are powerful because of what they get done with so little. Perl is a language that harnesses the power of minimal code, letting users automate otherwise mind-numbing tasks with a single line of code.

In Perl One-Liners, I take you through more than 100 compelling one-liners that do all sorts of handy and geeky things, such as manipulate line spacing, tally column values in a table, and get a list of users on a system. This cookbook of useful, customizable, and fun scripts will even help hone your Perl coding skills, as I dissect the code to give you a deeper understanding of the language.

Table of Contents

  • Acknowledgments
  • Appendix A: Perl's Special Variables
  • Appendix B: Using Perl One-Liners on Windows
  • Appendix C: perl1line.txt
  • Index (PDF)

View the detailed Table of Contents (PDF)

What are Perl One-Liners?

Perl one-liners are small and awesome Perl programs that fit in a single line of code. They do one thing really well—like changing line spacing, numbering lines, performing calculations, converting and substituting text, deleting and printing specific lines, parsing logs, editing files in-place, calculating statistics, carrying out system administration tasks, or updating a bunch of files at once. Perl one-liners will make you a shell warrior: what took you minutes (or even hours) to solve will now take you only seconds!

Let's take a look at several Perl one-liners to see how short and powerful they are. All these one-liners are explained in greater detail in the book.

Generate a random eight-letter password

perl -le 'print map { ("a".."z")[rand 26] } 1..8'

This one-liner generates and prints a random eight-character password. It uses the list range operator .. to produce all alphabet letters from a to z (for a total of 26 letters). Then a random letter is chosen by rand 26 and this operation is repeated 8 times to produce an eight-letter password.

Sum numbers in each line

perl -MList::Util=sum -lane 'print sum @F'

This one-liner uses the sum function from the List::Util module that comes with Perl. The -a command line argument enables the automatic splitting of the current line into fields in the @F array. The splitting happens on whitespace characters by default. The -l argument ensures that print outputs a newline at the end of each line. The -n argument makes sure every line is processed. Finally, sum @F sums all the elements in the @F array, and print prints the result followed by a newline (which I added with the -l argument).

Print all lines from line 17 to line 30

perl -ne 'print if 17..30'

This one-liner uses the binary flip-flop operator ... The operator is bistable, and it works just like the line-range (comma) operator in sed, awk, and various text editors. Its value is false as long as its left operand is false. Once the left operand is true, the range operator is true until the right operand is true, after which the range operator becomes false again. As a result, this bistable operator becomes true at line 17, stays true until the 30 line, and then becomes and remains false. Combining the flip-flop operator with print if condition makes it print only lines 17 to 30.

Finding all lines in a file that appear more than once

perl -ne 'print if $a{$_}++' file

This one-liner records the lines you've seen so far in the %a hash and counts the number of times it has seen the lines. If it has already seen the line, the condition $a{$_}++ is true, so it prints the line. Otherwise it automatically creates an element that contains the current line in the %a hash and increments its value.

Create 1KB of data

perl -e 'print "a"x1024'

This one-liner repeats the letter "a" 1024 times, creating 1KB of data. Operator x is the repetition operator. This one-liner is handy when you need to generate a specific amount of data for debugging or other tasks.

As you can see, knowing how to write one-liners is very useful. Through the years, it's been one of my top priority tasks to become very efficient in the shell. Literally every day when I'm programming, I've to do all kinds of data processing tasks, changing files, verifying output, doing quick calculations, parsing data, etc. Knowing Perl one-liners makes it really fast to get things done and be really productive. With Perl One-Liners you can now become very efficient at these tasks, too!


"By reading this book and trying the recipes, you’ll master shortly all the command-line options that can turn the Perl interpreter into a sed-awk-grep-toolboox-on-steroids."
—Alexis Sukrieh, creator of Perl Dancer (Read More)

“One of the slogans used by Perl is ‘Easy things should be easy and hard things should be possible.’ This book illustrates just how easy things can be—and how much can be done with so little code.”
—David Precious, contributor to the Perl Dancer project and various CPAN modules

“A great reference for when you want to do something with Perl without spending an hour or two trying to figure out how.”
—Sandra Henry-Stocker, IT World (Read More)

“By reading this book you can make a step toward becoming the local computer wizard, even without learning how to program.”
—Gabor Szabo, founder and editor of the Perl Weekly newsletter

“I didn't find anything to yell about. Given how much of a perfectionist bastard I am this is likely high praise.”
—Matt Trout, technical director of Shadowcat Systems

“A set of exercises for deepening your understanding of Perl.”
—John D. Cook, Singular Value Consulting

“The author is enthusiastic about the material and uses an easy writing style. Highly recommended.”
—Thrig (Jeremy Mates), Internet plumber

“These one-liners are great. Simple. Clear. Concise.”
—Jonathan Scott Duff, Perl guru

“A quick read full of useful command-line Perl programs.”
—Chris Fedde, systems engineer and Perl enthusiast

“Handy for anyone who does a lot of one-off text processing: system administrators, coders, or anyone with large amounts of data they need shifted, filtered, or interpreted.”
—Jim Davis, Perl developer

“Any Perl programmers should take the time to get to know the command like switches that make this possible. This book is a pretty good introduction to this way of using Perl.”
—David Cross, Perl Hacker, Trainer and Author (Read More)

“Seriously, folks, CompSci 101 courses might be more fun with a book like this (instead of the standard C++ tome).”
—Thomas Maher (Read More)

“Perl 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.”
—Nitin K Sookun (Read More)

“This book shows the undisputed king of one-line programs in it's full Swiss-Army-Chainsaw glory.”
—Kieran Barry (Read More)

“By the end of the book one should master enough of Perl code to write beautiful scripts.”
—Ish Sookun (Read More)

“Author pulls out a lot of interesting Perl tricks that someone can learn from. The book is worth reading just for that; it's also a short easy read.”
—John Graham-Cumming (Read More)

“If you are doing the sort of thing Perl excels at, like text mangling, this will be a book full of tools for you.”
—Justin Sherrill, DragonFly BSD (Read More)

“The presentation style is great as all the one liners are explained and alternatives presented, in keeping with the Perl idiom "There is more than one way to do it".”
—Andrew Colin Kissa, Creator of Baruwa, Fedora Linux contributor (Read More)

“Summing up, this is a useful book for admins, shell users and coders alike who need their tools concentrated into one toolbox and not spread here and there. Having the book to hand will allow you to call on Perl's full power in an instant when the need arises. Recommended”
—Nikos Vaggalis (Read More)

Book Preview

I've made Chapters 1 and 4 freely available:

You can also view full index of the book and detailed table of contents.

Buy Perl One-Liners

Perl one-liners is available from No Starch Press (use discount code CATONMAT for 30% off), Amazon, Amazon Kindle, O'Reilly, Powell's and Barnes and Noble.

Tweet about my book!

Help me spread the word about my new book! I prepared a special link that you can use to tweet about it:

What's next?

I really love writing about programming and I'll soon publish several more books. The next few are going to be about becoming skillful in bash (you can already start reading it here!), about the most effective vim tricks, a practical guide on how to be anonymous on the web, and the catonmat book.

If you enjoy my writing, you can subscribe to my blog. You can also follow me on Twitter or Google+.


November 28, 2013, 01:07

Wow! That's great!! Congratulations man!!
You deserve it!
Keep doing more of this fantastic job!!

Joe Permalink
November 28, 2013, 16:59

Happy Thanksgiving from America - yes, I know you probably don't celebrate it since it is an American holiday. Anyway, congratulations on your recent publication!

December 03, 2013, 22:17

I'm ordering today, can't wait to see if my pea size brain can understand and apply. I'm not ready to give up. Thank you!

Surio Permalink
December 04, 2013, 18:10

Very happy for you!

Well done on the achievement. In all these years of reading your blog, I have always admired your optimism and enthusiasm, i.e., looking onwards and forwards all the time. And it seems to pay off at regular intervals too! ;)

Congrats, and good luck for the future too.

December 25, 2013, 19:07

Congratulations! here's to more publications in the future!

peggyd Permalink
December 26, 2013, 05:07

Many students have also turned out to be prey to that sort of companies quite a few times during their academic studies. So beware of such online essay writing services and always seek to find the review of best essay writing sites whenever you require depending on the essay writing services to write your essays. Search on Google to buy essays online now. Best Essay Writing Review Sites are the easy and the best means for getting help of the high quality essay writing service.

May 28, 2014, 16:56

Its for free?

May 29, 2014, 08:49


May 31, 2014, 16:02

Keep it up!

June 16, 2014, 15:10


Kaur deep Permalink
December 04, 2014, 03:28

Excellent and knowledgeable links and post.thanks for sharing.
Now, The Time has come for the Happy New Year 2015. It wil bring a lot fo happinesin our life.
Happy New Year 2015
And I am sure, You wil anjoy this new year day with yoru fiends and wil send some images wishes,
New Year 2015

Happy New Year 2015 Wishes
Greetings to yoru close ones, Your friends, You lover, etc..
Happy New Year 2015 Images
Even I am waititng for that day, because it just changes our life.
Happy New Year 2015 Greetings

Happy New Year 2015 SMS
So, That's why I am commenting here to wish you a very happy new year.
Happy New Year 2015 Messages
I hope this New year 2015 wil bring success to your website. But before that Merry Christmas is coming.
Merry Christmas Quotes
It is also a big event for everyone.
Merry Christmas Pictures
Everyone knows that Christmas is a day when People seems to be very happy, they just leave all sadness behind.
Merry Christmas Wallpapers
And I am sure, you also enjoy this Merry Christmas day of 2014.
Christmas day Images

Christmas stocking
But if you are not looking out to enjoy this christmas, then you are missing out on a big thing, because it really brings happiness.
Merry Christmas Images

Merry Christmas 2014
So, what you are waiting for?
Merry Christmas 2015
Enjoy This Merry Christmas day. Cheers :).

December 07, 2014, 16:02

Congratulations :D Wish You More Success :)

December 07, 2014, 16:03

That's nice thanks for sharing

December 17, 2014, 03:54

thanks article i love your site

Diana Permalink
December 17, 2014, 16:02

This article is quite helpful and informative too. I enjoyed a lot. Thanks for sharing such a great article.

Beautiful Christmas Quotes for your friends and family...
christmas quotes
Christmas Messages for Whatsapp

Best Christmas Greetings for your friends and family...
christmas greeting
christmas wishes

What to Write in Christmas Card....... Check out best Christmas Greeting Card Words
What to Write in Christmas Card

Get Beautiful and Unique Christmas Wallpapers for free
free christmas wallpaper
christmas tree decorating ideas

Thanks for sharing such a great article.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "disk_401": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.