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

May 23, 2013

Decimal Sort in Perl


In the below mentioned, we have a hash with decimal values (for gpa key)
Lets assume we have a hash for students in the format mentioned below.
Let's discuss how to sort decimal values in Perl. We will sort both in ascending and descending ways.

decimal_sort.pl
use strict;
use warnings;
use Data::Dumper;

my %students = (
   'John'   => {
                    'Science'   => 'PASS',
                    'Maths'     => 'PASS',
                    'chemistry' => 'PASS',
                    'Physics'   => 'PASS',
                    'gpa'       => '4.01',
                  },

  'Diana'   => {
                     'Science'   => 'PASS',
                     'Maths'     => 'PASS',
                     'chemistry' => 'PASS',
                     'Physics'   => 'PASS',
                     'gpa'       => '4.10',
               },

  'David'    => {
                     'Science'   => 'PASS',
                     'Maths'     => 'PASS',
                     'chemistry' => 'PASS',
                     'Physics'   => 'PASS',
                     'gpa'       => '4.00',
                }
);

print "\n\n Before Sorting : ";
for my $each_student (sort keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}

print "\n\n After Sorting GPA in Ascending Order : ";
for my $each_student (sort{ $students{$a}{gpa} <=> $students{$b}{gpa} } keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}

print "\n\n After Sorting GPA in Descending Order : ";
for my $each_student (sort{ $students{$b}{gpa} <=> $students{$a}{gpa} } keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}
print "\n\n";
  


Output:

 Before Sorting : 
 Each Student : David   GPA : 4.00
 Each Student : Diana   GPA : 4.10
 Each Student : John    GPA : 4.01

 After Sorting GPA in Ascending Order : 
 Each Student : David   GPA : 4.00
 Each Student : John    GPA : 4.01
 Each Student : Diana   GPA : 4.10

 After Sorting GPA in Descending Order : 
 Each Student : Diana   GPA : 4.10
 Each Student : John    GPA : 4.01
 Each Student : David   GPA : 4.00
  


Please refer to other topics in Perl like :
Sort hash by value string in perl
Sort hash by value numerically in perl
Decimal Sort in Perl
How to read command line arguments in Perl
How to pass command line arguments to perl script
Loop through Directory and read the files in Perl
How to create excel report in Perl


Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count


Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File


Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics on Python like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton


Sort hash by value string in perl


If you want to sort numbers, use <=>
If you want to sort strings, use cmp instead of <=>

Sort hash by value(String) ascending in perl by using the following Syntax:
sort{ $hash{$a} cmp $hash{$b} }

Sort hash by value(String) descending in perl by using the following Syntax:
sort{ $hash{$b} cmp $hash{$a} }

sort_by_hash_value_strings.pl
use strict;
use warnings;
use Data::Dumper;

my %students = (
    Diana    => "Science",
    Davis    => "Science",
    John     => "Maths",
    Linus    => "Physics",
    Brown    => "Maths",
    Jane     => "Science"
);


print "\n\n Before Sorting on Value(Subject): ";
for my $each_student (sort keys %students) {
   print "\n Each Student : $each_student \tSubject : " . $students{$each_student};
}

print "\n\n After Sorting Value(Subject) in Ascending Order : ";
for my $each_student (sort{ $students{$a} cmp $students{$b} } keys %students) {
   print "\n Each Student : $each_student \tSubject : " . $students{$each_student};
}

print "\n\n After Sorting Value(Subject) in Descending Order : ";
for my $each_student (sort{ $students{$b} cmp $students{$a} } keys %students) {
   print "\n Each Student : $each_student \tSubject : " . $students{$each_student};
}
print "\n\n";  


Output:

 Before Sorting on Value(Subject): 
 Each Student : Brown   Subject : Maths
 Each Student : Davis   Subject : Science
 Each Student : Diana   Subject : Science
 Each Student : Jane    Subject : Science
 Each Student : John    Subject : Maths
 Each Student : Linus   Subject : Physics

 After Sorting Value(Subject) in Ascending Order : 
 Each Student : John    Subject : Maths
 Each Student : Brown   Subject : Maths
 Each Student : Linus   Subject : Physics
 Each Student : Jane    Subject : Science
 Each Student : Davis   Subject : Science
 Each Student : Diana   Subject : Science

 After Sorting Value(Subject) in Descending Order : 
 Each Student : Jane    Subject : Science
 Each Student : Davis   Subject : Science
 Each Student : Diana   Subject : Science
 Each Student : Linus   Subject : Physics
 Each Student : John    Subject : Maths
 Each Student : Brown   Subject : Maths



Please refer to other topics in Perl like :
Decimal Sort in Perl
Sort hash by value string in perl
Sort hash by value numerically in perl
How to read command line arguments in Perl
How to pass command line arguments to perl script
Loop through Directory and read the files in Perl
How to create excel report in Perl


Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count


Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File


Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics on Python like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton


Sort hash by value numerically in perl


If you want to sort numbers, use <=>
If you want to sort strings, use cmp instead of <=>

Sort hash by value(Number) ascending in perl by using the following Syntax:
sort{ $hash{$a} <=> $hash{$b} }

Sort hash by value(Number) descending in perl by using the following Syntax:
sort{ $hash{$b} <=> $hash{$a} }


sort_by_hash_value_numbers.pl

use strict;
use warnings;
use Data::Dumper;

my %students = (
   'John'   => {
                    'Science'   => 'PASS',
                    'Maths'     => 'PASS',
                    'chemistry' => 'PASS',
                    'Physics'   => 'PASS',
                    'gpa'       => '4.01',
                  },

  'Diana'   => {
                     'Science'   => 'PASS',
                     'Maths'     => 'PASS',
                     'chemistry' => 'PASS',
                     'Physics'   => 'PASS',
                     'gpa'       => '4.10',
               },

  'David'    => {
                     'Science'   => 'PASS',
                     'Maths'     => 'PASS',
                     'chemistry' => 'PASS',
                     'Physics'   => 'PASS',
                     'gpa'       => '4.00',
                }
);

print "\n\n Before Sorting : ";
for my $each_student (sort keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}

print "\n\n After Sorting GPA in Ascending Order : ";
for my $each_student (sort{ $students{$a}{gpa} <=> $students{$b}{gpa} } keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}

print "\n\n After Sorting GPA in Descending Order : ";
for my $each_student (sort{ $students{$b}{gpa} <=> $students{$a}{gpa} } keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}
print "\n\n";  


Output:

 Before Sorting : 
 Each Student : David   GPA : 4.00
 Each Student : Diana   GPA : 4.10
 Each Student : John    GPA : 4.01

 After Sorting GPA in Ascending Order : 
 Each Student : David   GPA : 4.00
 Each Student : John    GPA : 4.01
 Each Student : Diana   GPA : 4.10

 After Sorting GPA in Descending Order : 
 Each Student : Diana   GPA : 4.10
 Each Student : John    GPA : 4.01
 Each Student : David   GPA : 4.00



Please refer to other topics in Perl like :
Decimal Sort in Perl
Sort hash by value string in perl
Sort hash by value numerically in perl
How to read command line arguments in Perl
How to pass command line arguments to perl script
Loop through Directory and read the files in Perl
How to create excel report in Perl


Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count


Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File


Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics on Python like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton


May 13, 2013

How to read command line arguments in Perl

The following script will take the command line arguments and print one by one.

By default, all the command line argumnets are captured in @ARGV

Here, we directly pass the arguments without any labels

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

foreach my $arg (@ARGV) {
   print $arg . "\n";
}
1;	 

Output:

perl read_command_line_params.pl 100 "Mother Teresa"
100
Mother Teresa	 


Please refer to other topics in Perl like :
How to pass command line arguments to perl script
Loop through Directory and read the files in Perl
How to create excel report in Perl


Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count


Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File


Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics on Python like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton


How to pass command line arguments to perl script


We can pass command line arguments with the labels and we can make use of Getopt::Long module for achieving this

If an user tries to pass a wrong/invalid parameter, the following will throw an error.

The following script takes the parameters like
    -str (String value)
    -how_many_times (integer)
   
The following will print the string (from -str parameter) as many times the user the entered -how_many_times parameter

pass_command_line_params.pl
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use File::Basename;

#Extract the nae of this script. 
my $scriptname = basename($0);

my @usage =("$scriptname",
           "-str  = Enter a String",
           "-how_many_times = Enter an integer, how many times you want the String to print");

my $opt_help     = 0;
my $opt_str     = ""; 
my $opt_how_many_times  = ""; 

my $ret = &GetOptions('help',  \$opt_help,
                      'str=s',\$opt_str,
                      'how_many_times=i',\$opt_how_many_times);


# Check parameters
if (($ret != 1) || (! $opt_str) || (! $opt_how_many_times)){
    &usage();
    exit;   
}

sub usage {
    print "\n How to use this Script : ". Dumper(\@usage) . "\n";

    print "\n You didn't pass str parameter " if (!$opt_str);
    print "\n You didn't pass how_many_times parameter " if (!$opt_how_many_times);
    print "\n";

    exit;
}


for (my $i=0; $i<$opt_how_many_times; $i++) {
    print $opt_str. "\n";
}
1;  

Outout :
1) perl pass_command_line_params.pl -str=Peter -how_many_times=
    Value "dd" invalid for option how_many_times (number expected)

    How to use this Script : $VAR1 = [
              'pass_command_line_params.pl',
              '-str  = Enter a String',
              '-how_many_times = Enter an integer, how many times you want the String to print'
            ];


    You didn't pass how_many_times parameter  

2) perl pass_command_line_params.pl -str="Mother Teresa" -how_many_times=5
    Mother Teresa
    Mother Teresa
    Mother Teresa
    Mother Teresa
    Mother Teresa  



Please refer to other topics in Perl like :
Loop through Directory and read the files in Perl
How to create excel report in Perl


Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count


Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File


Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics on Python like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton