Skip to Content
Perl One-Liners
book

Perl One-Liners

by Peteris Krumins
December 2013
Intermediate to advanced
168 pages
2h 49m
English
No Starch Press
Content preview from Perl One-Liners

Chapter 7. Selectively Printing and Deleting Lines

In this chapter, we’ll examine various one-liners that print and delete certain lines. These one-liners will, for example, print repeated lines, print the shortest line in a file, and print lines that match certain patterns.

But every one-liner that prints certain lines can also be viewed as one that deletes the lines that aren’t printed. For example, a one-liner that prints all unique lines deletes all repeated lines. I’ll discuss only the one-liners that print something, rather than delete something, because one is always the inverse of the other.

7.1 Print the first line of a file (emulate head -1)

perl -ne 'print; exit' file

This one-liner is quite simple. Perl reads the first line into the

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl Best Practices

Perl Best Practices

Damian Conway
Perl Cookbook

Perl Cookbook

Tom Christiansen, Nathan Torkington
Minimal Perl

Minimal Perl

Tim Maher
Learning Perl, 7th Edition

Learning Perl, 7th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix

Publisher Resources

ISBN: 9781457185281Errata