Tuesday, June 30, 2009

Perl Regex - Example 1

Case: I have a file which reads like below. I need to remove the date stamp & also the "[filername: ses.exceptionShelfLog:info]:" text.


$ cat filername_exception_error | tail
Thu Jun 4 09:43:40 PDT [filername: ses.exceptionShelfLog:info]: Shelf Log information on channel 0d ESH module A shelf id 1.
Thu Jun 4 09:43:47 PDT [filername: ses.exceptionShelfLog:info]: Shelf Log information on channel 0d ESH module A shelf id 1.
Thu Jun 4 09:43:49 PDT [filername: ses.exceptionShelfLog:info]: Shelf Log information on channel 0c ESH module A shelf id 1.
Thu Jun 4 09:50:20 PDT [filername: ses.exceptionShelfLog:info]: Shelf Log information on channel 0d ESH module B shelf id 4.
Thu Jun 4 09:50:40 PDT [filername: ses.exceptionShelfLog:info]: Shelf Log information on channel 0d ESH module A shelf id 5.
Thu Jun 4 09:50:51 PDT [filername: ses.exceptionShelfLog:info]: Shelf Log information on channel 0d ESH module B shelf id 5.
Thu Jun 4 09:51:01 PDT [filername: ses.exceptionShelfLog:info]: Shelf Log information on channel 0d ESH module B shelf id 6.

The regular expression to do this is:
$ cat filername_exception_error  | perl  -pe "s/\d\d:\d\d:\d\d//g" |  perl -i  -pe "s/\[filername: ses.exceptionShelfLog:info]://g" | tail 
Thu Jun 25 PDT Shelf Log information on channel 4d ESH module A shelf id 2.
Thu Jun 25 PDT Shelf Log information on channel 4d ESH module A shelf id 3.
Thu Jun 25 PDT Shelf Log information on channel 4d ESH module B shelf id 1.
Thu Jun 25 PDT Shelf Log information on channel 4d ESH module B shelf id 2.
Thu Jun 25 PDT Shelf Log information on channel 4d ESH module B shelf id 3.
Fri Jun 26 PDT Shelf Log information on channel 4d ESH module B shelf id 2.
Sat Jun 27 PDT Shelf Log information on channel 4d ESH module B shelf id 2.
Sun Jun 28 PDT Shelf Log information on channel 4d ESH module B shelf id 2.
Mon Jun 29 PDT Shelf Log information on channel 4d ESH module B shelf id 2.
Tue Jun 30 PDT Shelf Log information on channel 4d ESH module B shelf id 2.

Friday, June 5, 2009

If loop - numerical vs string comparison

Lets see the difference between numerical comparison and string comparison:

Simplest example of if-else loop


#!/usr/bin/perl

$a=9;
$b=35;

print "CASE 1: Following is using numerical comparison operator \n";
if ( $a > $b ) {
print "$a is greater than $b \n";
} else {
print "$a is less than $b \n";
}

print "\n\n";

print "CASE 2: Following is using string comparison operator \n";
if ( $a gt $b ) {
print "$a is greater than $b \n";
} else {
print "$a is less than $b \n";
}


The output is as shown below: (in one case 9 is smaller than 35 and in other case, opposite)

c:\>perl script3.pl
CASE 1: Following is using numerical comparison operator
9 is less than 35


CASE 2: Following is using string comparison operator
9 is greater than 35

Basic operations - Arithmatic & string

Code:


#!/usr/bin/perl
# Scaler Variables operations
$a=7;
$b=3;
$str1="abc";
$str2="def";

system (clear);
print "################## BASIC OPERATIONS IN PERL ################## \n";
print "\n\n#################### Arithmatic Operators #################### \n";

$c = $a + $b;
print "Addition \t\t\t: $a + $b = $c \n";

$d = $a * $b ;
print "Multiplication \t\t\t: $a * $b = $d \n";

$e = $a / $b ;
print "Division \t\t\t: $a / $b = $e \n";

$f = $a % $b;
print "Modulus \t\t\t: $a % $b = $f \n";

$g = $a - $b;
print "Subtraction \t\t\t: $a - $b = $g \n";

print "\n\n######### String operators - when values are numeric ######### \n";

$i = $a . $b;
print "Concatenation \t\t\t: $a . $b = $i \n";

$j = $a x $b;
print "Duplication \t\t\t: $a x $b = $j \n";

print "\n\n######### String operators - when values are strings ######### \n";

$str3 = $str1 . $str2 ;
print "Concatenation \t\t\t: $str1 . $str2 = $str3 \n";

$str4 = $str1 x 4 ;
print "Concatenation + multiplication \t: $str1 x 4 = $str4 \n";


print "\n\n\n\n";




Result:

################## BASIC OPERATIONS IN PERL ##################


#################### Arithmatic Operators ####################
Addition : 7 + 3 = 10
Multiplication : 7 * 3 = 21
Division : 7 / 3 = 2.33333333333333
Modulus : 7 % 3 = 1
Subtraction : 7 - 3 = 4


######### String operators - when values are numeric #########
Concatenation : 7 . 3 = 73
Duplication : 7 x 3 = 777


######### String operators - when values are strings #########
Concatenation : abc . def = abcdef
Concatenation + multiplication : abc x 4 = abcabcabcabc

Thursday, June 4, 2009

Hello World!

Code:


$cat script1.pl
#!/usr/bin/perl
print "Hello World! \n" ;


Result:


$perl script1.pl
Hello World!

About Me

My photo
Unix geek with AWS, Cloud computing, Linux, Netapp, VMware, Scripting background! :)