May 29, 2013

Perl one liners

Most popular Perl One Liners

Sort File:
perl -e 'print sort {lc($a) cmp lc($b)} <>' input.txt > output.txt

Print Reverse of File:
perl -e 'print reverse <>' input.txt

Substitute the occurrences in a file:
perl -pi -e 's/foo/bar/g' input.txt      (It won't take backup)
perl -pi.bak -e 's/foo/bar/g' input.txt  (It will take backup)

Remove Duplicates and print the contents of a File :
perl -ne '$H{$_}++ or print $_' testfile.txt

Don't confuse with -n and -p switches
-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 '$_'
-p switch
  The 'p' switch is similar to 'n' switch (while loop around program) but it has an advantage of printing the line automatically.


You can use the following perl one lines from unix command line
(Reference: http://www.alfoos.com/perl-one-liners-howto/)

-e switch 
Allows you to pass a script to STDIN
Allows you to provide the program as an argument rather than in a file.
You don't want to have to create a script file for every little Perl one-liner.

# command-line that reverses the whole file by lines
         perl -e 'print reverse <>' input.txt

Note:
<> in list context returns all the lines in the file

How to Sort a file which contains strings (without case) in Ascendig order
perl -e 'print sort {lc($a) cmp lc($b)} <>' input.txt > output.txt

How to Sort a file which contains strings (without case) in Descendig order
perl -e 'print sort {lc($b) cmp lc($a)} <>' input.txt > output.txt

How to Sort a file which contains strings (with case) in Ascending Order
perl -e 'print sort {$a cmp $b} <>' input.txt > output.txt

How to Sort a file which contains strings (with case) in Descending Order
perl -e 'print sort {$b cmp $a} <>' input.txt > output.txt

How to Sort a file which contains numbers (Ascending Order)
perl -e 'print sort {$a <=> $b} <>' input.txt > output.txt

How to Sort a file which contains numbers (Decending Order)
perl -e 'print sort {$b <=> $a} <>' input.txt > output.txt

Simple Sort:
perl -we 'print sort <>' input.txt > output.txt

Sort lines by their length
perl -e 'print sort {length $a <=> length $b} <>' input.txt

-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 '$_'
 
   perl -ne 'print' input.txt   //Correct
   or
   perl -ne 'print $_' input.txt  //Correct

   perl -e 'print $_' input.txt  //Wrong

   # Delete first 10 lines 
   perl -i.old -ne 'print unless 1 .. 10' input.txt
     
   # Just lines 15 to 25 
   perl -ne 'print if 15 .. 25' input.txt
 
   Remove all blank lines of a file (-i will write into same file)
   perl -ni -e 'print unless /^$/' input.txt
 
   Remove all blank lines of a file with backup file
   perl -ni.bak -e 'print unless /^$/' input.txt
 
 
-p switch
   The 'p' switch is similar to 'n' switch (while loop around program) but it has an advantage of printing the line automatically.
 
   perl -pi -e 's/foo/bar/' *.txt
 
   perl -pe 'print' input.txt  //The lines are printed twice, once by 'p' switch and once by 'print'.

   perl -pi -e 's/foo/bar/g' input.txt (It won't take backup)
 
   perl -pi.bak -e 's/foo/bar/g' input.txt  (It will take backup)
 
-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.
   Modifies your input file in-place (making a backup of the original).
   Handy to modify files without the {copy, delete-original, rename} process.

   perl -i -pe 's/foo/bar/;' input.txt  //modifies in the same file (don't print to STDOUT)

   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/foo/bar/;' input.txt  //This take the backup

   In Perl, how to do you remove ^M from a file?
   perl -p -i -e 's/\r\n$/\n/g' file1.txt file2.txt  

-M switch 
    Although it is possible to use a -e option to load a module, Perl gives you the -M option to make that easier.

        perl -MData::Dumper -e 'my %hash = ("a" => 10, "b" => 20, "c" => 30);  print "\n Dump Value: " . Dumper(\%hash)'
perl -MCGI -e 'print "$CGI::VERSION \n"'
perl -MData::Dumper -e 'print "$Data::Dumper::VERSION \n"'

-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
This will append " appended to line" in second line, this is because we havnt added 'l' switch and the string was added after '\n'.

perl -lne 'print $_." appended to line"' testfile.txt
This will " appended to line" correctly, this is because the 'l' switch removes the '\n' first then append the string and then add '\n' before printing

-a switch 
    Awk commands
    echo "Hello World" | awk '{print $2}'
    echo "Hello guys" | perl -lane 'print $F[1]'

-w switch 
   It is the same as use warnings

-d switch
   for debugging

No comments:

Post a Comment