11 Perl One-liner Script Examples Beginners Guide
11 Perl One-liner Script Examples, A very interesting feature of the Perl programming language is its ability to execute condensed (one line) versions of otherwise complete perl scripts including but not limited to modifying files, replacing text etc. On many of the older UNIX based operating systems the installed versions of awk/sed/grep may not support all the text manipulation features of their modern updated counterparts or we may not have GNU versions of these packages available. In such situations Perl one liners can prove to be a worthy replacement.
Author Of this article and Credits to Sahil Suri
The basic syntax for a Perl one liner is as follows:
perl <flag> <code>
- The keyword perl invokes the perl interpreter.
- The flag are the options that we’d like to specify to dictate how the code should run and the code is our actual perl logic.
To get information on how to use perl one liners on the command line just type perldoc perl run as shown below:
[root@arkit-centos ~]# perldoc perlrun | more NAME perlrun - how to execute the Perl interpreter SYNOPSIS perl [ -sTtuUWX ] [ -hv ] [ -V[:configvar] ] [ -cw ] [ -d[t][:debugger] ] [ -D[number/list] ] [ -pna ] [ -Fpattern ] [ -l[octal] ] [ -0[octal/hexadecimal] ] [ -Idir ] [ -m[-]module ] [ -M[-]'module...' ] [ -f ] [ -C [number/list] ] [ -S ] [ -x[dir] ] [ -i[extension] ] [ [-e|-E] 'command' ] [ -- ] [ programfile ] [ argument ]...
I’ve excluded most of the output because this gives a lot of information.
Let’s write our first one liner which is obviously hello world!
11 Perl One-liner Script Examples
1 Example : Hello world
[root@arkit-centos ~]# perl -e 'print "Hello World!\n"' Hello World!
The -e option means execute. This option will always be specified as a flag for every perl one liner. Also if we are using multiple flags then in that case the -e flag must be written lastotherwise the code will not execute.
Perl is highly flexible when it comes to quotes. We can use q and qq with different delimiters to represent single and double quotes respectively.
Here are a few examples:
2 Example : Using qq|| to represent double quotes.
[root@arkit-centos ~]# perl -e 'print qq|Hello World!\n|' Hello World!
3 Example : Using q|| to represent single quotes.
[root@arkit-centos ~]# perl -e 'print q|Hello World!|' Hello World! [root@arkit-centos ~]#
4 Example : Using both qq and q in the same one line program
[root@arkit-centos ~]# perl -e "print q|'Hello World'|; print qq|\n| " 'Hello World'
Now if we did not want to specify the newline manually then we could use -l option called the line feed flag. This operates like the chomp function we use within looping structures in perl programs. We often use the line feed flag in conjunction with while loops in perl one liners as well and I’ll get to that shortly.
We can use the -p option to loop over a file served as input to the perl interpreter and print it out.
I have a file named ptest.
[root@arkit-centos ~]# cat ptest unix solaris unix aix unix hpux linux centos linux fedora linux ubuntu
5 Example: Using perl with the -pe options to print the content of a file to stdout.
[root@arkit-centos ~]# perl -pe ' ' ptest unix solaris unix aix unix hpux linux centos linux fedora linux ubuntu
Now we get to looping and this is where the magic really starts.
Example 6: Using a while loop within perl one liner.
So if I wanted to loop through the file ptest, search for the word solaris and print it I could do:
[root@arkit-centos ~]# perl -le 'while (<>) {print if m^solaris^}' ptest unix solaris
Example 7: Use the -n flag to signify a while loop and save some typing.
[root@arkit-centos ~]# perl -nle 'print $_ if m^unix^' ptest unix solaris unix aix unix hpux
- This searches the file for the word unix and prints out the matches.
- The $_ is the special variable which gets the value of the line being processed.
- m signifies a regex match and ^ ^ are the delimiters.
- I’d like to add here that I’ve used post condition here wherein the statement to be executed if the condition is true is written before the condition itself. Yes! We can do that in perl!
- In fact I don’t even need to mention that I need to print $_. Perl can work on an assumption
if I leave it empty like I’ve done here:
Example 8: Omit the $_ and let perl use it as default.
[root@arkit-centos ~]# perl -nle 'print if m^unix^' ptest unix solaris unix aix unix hpux
Next I’d like to talk about BEGIN and END blocks. These have a similar function in perl as they do in awk if you’re familiar with that. BEGIN & END blocks get executed only at the beginning and at the end of the perl script/code/logic in this case the while loop. 11 perl one-liner script
Example 9: Using END block.
[root@arkit-centos ~]# perl -nle 'print && $count++ if m^unix^ ; END {print $count}' ptest unix solaris unix aix unix hpux 3
This one liner will printing the word unix and the value of variable $count is incremented every time a match is found. The && operator does the print and increment together in one go if the condition is true. Again, I’m using post conditionals here. Inside the end block I’ve printed out the value of $count. The end block is executed after the while loop has run its course. Since perl is awesome, it assumes that the initial value of $count is 0 since I didn’t assign anything.
Example 10: Using BEGIN block to create a basic grep program.
UNIX/LINUX admins are very familiar with the use of grep search for stuff within text files.
This next one liner is like a perl variant of a grep search:
[root@arkit-centos ~]# perl -nle 'BEGIN { $regex = shift} print $+ if m~($regex)~' 'unix' ptest unix unix unix
Within the BEGIN block I assign the value unix to the variable $regex. I’ll explain how. When we supply arguments to a perl script they are stored within an array called ARGV. When I did the shift perl shifted the first value in ARGV which was unix and assigned it to the variable $regex. Then the if condition is matched for the value of $regex which is unix and the matches are printed. In this we’ve also done a grouping regex match so only the matched word got printed and not the complete line. 11 perl one-liner script
If I wanted to print the entire line and not just the matched string then here are a couple of examples on doing that:
[root@arkit-centos ~]# perl -nle 'BEGIN { $regex = shift} print $_ if m~$regex~' 'unix' ptest unix solaris unix aix unix hpux [root@arkit-centos ~]# perl -nle 'BEGIN { $regex = shift} print $_ if m~$regex~' 'unix\ss' ptest unix solaris [root@arkit-centos ~]# perl -nle 'BEGIN { $regex = shift} print $_ if m~$regex~' 'unix\s.*?s$' ptest unix solaris [root@arkit-centos ~]# perl -nle 'BEGIN { $regex = shift} print $_ if m~$regex~' 'unix\s.*?x$' ptest unix aix unix hpux [root@arkit-centos ~]# [root@arkit-centos ~]# perl -nle 'BEGIN { $regex = shift} print $+ if m~($regex)~' 'unix\s.*?x$' ptest unix aix unix hpux
One may argue that using grep is much simpler than what I did above with perl and for the most part it is. But if we get down to some nasty regex searches/matches then grep may not be enough . 11 perl one-liner script
Example 11: Using in place file editing with the –i option.
For the last example of one liners I’ll demonstrate in place editing. This is analogous to using sed with the -i option.
Using the file ptest again for this example.
[root@arkit-centos ~]# cat ptest unix solaris unix aix unix hpux linux centos linux fedora
linux ubuntuLet’s replace all occurrences of the word unix with UNIX and keep a backup of the original file.
[root@arkit-centos ~]# perl -pi.bkp -e 's/unix/UNIX/g' ptest [root@arkit-centos ~]# cat ptest UNIX solaris UNIX aix UNIX hpux linux centos linux fedora linux ubuntu
[root@arkit-centos ~]# cat ptest.bkp unix solaris unix aix unix hpux linux centos linux fedora linux ubuntu
And that’s it!
Note that since this was a search and replace operation there was no output printed to the terminal like we have in the case of sed.
Related Articles
For More Articles follow Sahil Suri
Thanks for your wonderful Support and Encouragement