Perl
Perl one-liners HowTo
Perl one-liners HowTo
Perl one-liners are very useful for system administrators. Here I will explain each switch in the perl command line.
The 'e' switch
The 'e' switch is for writing a one-liners program, it omits program file. Code can be written inside ' or ".
perl -e 'print "Hello World"';
perl -e '$a=1;$b=2;$c=$a+$b;print $c;'
The 'n' switch
The 'n' switch will automatically add a while loop in your one-liner. This will help you to process each input line one by one. Each input line will be in '$_'
A test file
----------testfile.txt----
Hi guys
How are you
----------
To simply print the file
--------
perl -ne 'print' testfile.txt
or
perl -ne 'print $_' testfile.txt
--------
Note that following will not work
-----
perl -e 'print' testfile.txt
------
The 'p' switch
The 'p' switch is similar to 'n' switch (while loop around program) but it has an advantage of printing the line automatically.
So to print the file you can use
--------
perl -pe '' testfile.txt
--------
Here you are not printing explicitly like when you used 'n' switch. The lines are automatically printed by 'p' switch.
See what happens here
--------
perl -pe 'print' testfile.txt
--------
The lines are printed twice, once by 'p' switch and once by 'print'.
Note that the 'p' switch prints the value in '$_' after executing your one-liner. That is why this works
----------
perl -pe 's/Hi/Hello/;' testfile.txt
----------
To make the above one-liner work with 'n' switch you need to print explicitly.
----------
perl -ne 's/Hi/Hello/;print' testfile.txt
----------
and this will not print any output
------
perl -ne 's/Hi/Hello/;' testfile.txt
------
Also verify this
--------
perl -pe '$a=$_; $a =~ s/Hi/Hello/;print $a' testfile.txt
--------
The 'i' switch
The 'i' switch is to do the in-line editing of file. As you can see all the one-liners above prints to STDOUT. Using 'i' switch you can print it to the same file from which you are reading. When you use 'i' switch you will not see the output in STDOUT but the output will be printed to the reading file itself.
----------
perl -i -pe 's/Hi/Hello/;' testfile.txt
----------
If you run below script you will see testfile.txt as empty
-------
perl -i -ne 's/Hi/Hello/;' testfile.txt
-------
This is because 'n' switch do not print anything like 'p' switch. To make that work you need to print explicitly.
---------
perl -i -ne 's/Hi/Hello/;print' testfile.txt
---------
You can also pass an extension to 'i' switch. This will create a backup of your original file with the given extension before editing the original file
--------
perl -i.bk -pe 's/Hi/Hello/;' testfile.txt
--------
Now you will see your original file saved as testfile.txt.bk and edited contents in testfile.txt
The 'l' switch
The 'l' switch is for processing the line terminator. As you know the default line terminator is '\n'. You can manage the line terminator using the 'l' switch. You need to pass an octal value along 'l'. First it does a chomp of the input records which removes that character. Second, when doing a print it adds that character to the end of each line (by setting the $\ output record separator). If you do not pass any octal value then '\n' will be assumed.
--------
perl -ne 'print $_." appended to line"' testfile.txt
perl -lne 'print $_." appended to line"' testfile.txt
perl -l0ne 'print;' testfile.txt
perl -l0ne 'print $_." appended to line"' testfile.txt
--------
The first one will print the line and will append " appended to line" in second line, this is because we havnt added 'l' switch and the string was added after '\n'.
The second will add " appended to line" correctly, this is because the 'l' switch removes the '\n' first then append the string and then add '\n' before printing(here we haven't changed the line terminator using a octal value hence the default is '\n').
The third will print everything in one line by omitting '\n', this is because we have passed an octal value 0 as the line terminator.
The following will not have any difference since you are not passing any octal value to 'l' switch nor appending anything to the end of the string.
--------
perl -lpe 's/Hi/Hello/;' testfile.txt
--------
The 'a' switch
The 'a' switch is to auto split the input line. This is to emulate the awk functionality. As you know we can print columns using awk like
-------
echo "Hello guys" | awk {'print $2'}
-------
By using 'a' switch you can do this in perl.
-------
echo "Hello guys" | perl -lane 'print $F[1]'
-------
By default the 'a' switch will split the input line with respect to ' ' (space) and stores the result in the array '@F'. So if the input string is 'Hello guys', 'Hello' will be in $F[0], 'guys' will be in $F[1] etc.
The 'F' switch
The 'F' switch is used in conjunction with 'a' switch. As I said 'a' switch splits input line with respect to ' '(space), what if we want to change the split pattern ' '(space) to something else? You can do this by passing the split patter with the 'F' switch
---------
echo "Hello:guys" | perl -lanF/:/ -e 'print $F[0]'
---------
Here I have changed the split pattern from ' '(space) to ':'. The //'s are optional, you can pass pattern without //, then anything after 'F' will be considered as the pattern.
--------
echo "Hello:guys" | perl -lanF: -e 'print $F[0]'
---------
Note the below script will not run
--------
echo "Hello:guys" | perl -lanF:e 'print $F[0]'
--------
Here the 'F' switch took ':e' as the pattern and didn't recognized 'e' as the switch. So when using 'F' switch better use 'e' switch separately like
perl -lanF: -e