Perl ProgrammingLearn about One Liners in Perl Programming

Learn about One Liners in Perl Programming

Perl-One

A One-Liner is a very short program that fits within one-line, and could do a job, usually done by longer programs. In this context, Perl is considered one of the richest programming languages supporting one-line programs. This topic will be the focus of our today’s article. Have a nice reading.

The –e Option

The –e option causes the Perl interpreter to execute a line of code from the command line.

Syntax

perl –e “COMMAND” [ARGUMNETs]

Concatenating Files using One-Liners

When talking about the use of the diamond operator <> in “Article (14) – Input and Output (2)”, we have written a script that implements the function of the well-known UNIX/Linux command cat:

#!/usr/bin/perl
while(<>)
{
        print $_;
}

You may remember that in a previous blog post,  I told you that one day I will show you how to do the same job using only one line of code. Here we are.
1

That printed the content of one file. What about concatenating two files?
2

– Wow. That is Magical!!!

Actually, no magic at all!! You know, this one-liner is just a squeezed version of the above program. Consider re-writing the above program as follows:

#!/usr/bin/perl
while(<>) { print $_; }

Even more, the Shebang line could be omitted if we are going to invoke the Perl interpreter explicitly when running the script file. This way, the script could be:

while(<>) { print $_; }

and run as:

perl cat.pl fileA fileB fileC

To do all the work from command line, we need to tell the interpreter that the following is a Perl instruction (not a script filename). This is the role of the –e option. The Perl instruction following the –e needs to be enclosed within pair of quotes.

perl -e "while(<>) { print; }" fileA fileB fileC

Surprisingly, this one-liner can be made even shorter.

The –n Option

The –n option tells the interpreter to assume the whole program as the body of a while loop that iterates over each line of specified input files. In other words, the concatenation program:

#!/usr/bin/perl
while(<>)
{
        print $_;
}

Could be re-written as:

#!/usr/bin/perl -n
print $_;

So, the one-liner now could be:

perl -ne "print;" fileA fileB fileC

Even shorter?! Huh?!

The –p Option

The –p option has the same function as –n option, with one difference that it “both” reads and prints lines read from the input files. Accordingly, the one-liner could be written without need to specify the print action:

perl -pe ";" fileA fileB fileC

That is what I call magic!

Editing Files using The –i Option

The –i option edits files in place.

Example
The following one-liner converts all letters in the input file(s) from lowercase to uppercase:

perl -p -i -e "tr/a-z/A-Z/;" quote1

Consider the file quote1, which contains a nice quote by the sportsman of the century Mohamed Ali.
3

Now, using the above one-liner should convert all letters in the files to uppercase.
4

Saving Backups of Input Files

If an extension is provided, the –i option takes a backup copy of each file before modifying it.

Example
Consider another nice quote by Mohamed Ali written in file quote2. We need to backup the file before converting its letters from lowercase to uppercase. Watch this:

We have only quote2 file:
5

It contains the following quote:
6

Now, try executing the following line:

perl -p -i.orig -e "tr/a-z/A-Z/;" quote2

It should modify the quote2 file, after saving an .orig copy of it:
7

The original file should now be:
8

While the backup copy is:
9

Learn the Basics of C Programming Language

Splitting Lines of Text and Extracting Fields

The Perl one-liners can also do the function of the well-known UNIX/Linux commands cut and awk that work as field extractors. The option –a enables the line-splitting feature of Perl. Together with option –n or –p, the –a option splits the input lines of text into sets of words, assuming white spaces as the field separators. This is the default behavior. To specify another pattern as field separator, the –F option is used followed by the pattern. In all cases, the resulting list is stored in the special array @F.

Example
Starting with a file that contains the names of some superstar footballers:
10

To extract the list of first names:
11

Where $F[0] is the first element in the special array @F.

To extract the list of last names:
12

Line Numbering

Perl has a special variable $. that holds the line number of the current line of the input file (or command output piped as input).

Examples

  • The following command displays the contents of the players file, with line numbers added next to each line (this is equivalent to using the switch –n with the cat command)

 

perl -ne 'print $.,"\t",$_;' players

13

  • The following one-liner lists processes started by the apache user, and puts a line number next to each process:

 

ps -f -u apache | perl -ne 'print $.,"\t",$_;'

14

Summary
In this article, we have tackled One-Liners.

  • A one-liner is a one-line short program that does a useful task.
  • The –e option is used to instruct the Perl interpreter to treat the following Perl statement as a program to execute from the command-line shell.
  • The –n option assumes a “while (<>) { … }” loop around program.
  • The –p is similar to –n, with an extra option: it prints the line.
  • The –i option allow editing input file(s) in place. It can also save a backup copy of the file, if an extension is provided.
  • The –a option enables the line-splitting function in Perl.
  • The special variable $. contains the line number of the current line of input.

In the next article, we are going to talk about “Managing System Processes”. An interesting topic to wait for. See you.

2 COMMENTS

  1. Yeah, it is good, but the webpage puts the [fb, twitter, in] boxes at the left in my browser, obscuring the meat of the article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -