Saturday, February 5, 2011

PERL : Regular Expressions

pico re1.pl
#!/usr/bin/perl -w
#anything between '/ /' is considerated by perl a regular expresion
#perl parse the content of $_
$firstname = "Mark";
if ($firstname =~ /Mark/)
{
print "TRUE";
}
if ($firstname =~ /mark/i)
{
print "TRUE";
}
#Anchor Tags - ^ = search at the begining of the string
#Anchor Tags - $ = search at the end of the string
$fullname = " Mark Sloan";
if ($fullname =~ /^mark/i)
{
print $fullname;
}
@array1 = ("Mark Sloan","Sloan Mark");
foreach(@array1)
{
if (/^Mark/i)
{
print $_\n;
}
}
foreach(@array1)
{
s/Mark/Marko/g;#using 'g' will work globally
print $_;
}
#end


#!/usr/bin/perl -w
$IN = "INFILE";
$OUT = "OUTFILE";
$FILENAMEIN = "data1";
$FILENAMEOUT = "data2";
open ($IN, "$FILENAMEIN") || die "Problems opening file: $!";
open ($OUT, "$FILENAMEOUT") || die "Problems creating file : $!";
@filecontents = <$IN>;
foreach (@filecontents)
{
if(/Mark/i)
{
s/Mark/Marko/;
print $OUT "$_";
}
}
#end


Groping
#!/usr/bin/perl -w
$var1 = "Linux 1 2 3 4";
#\d extracts only digits
$var1 =~ /(\w*)(\s\d)(\s\d)(\s\d)(\s\d)/;#returns all digits and the text
print $1 $2 $3 $4 $5; 
#end


date -> Sun Oct 12 13:45:34 EDT 2011
date +%r -> 1:45:34 PM
#!/usr/bin/perl -w
$time =`date +%r`;
$time =~ /(\d\d):(\d\d):(\d\d)\s(.*)/;
print $1 $2 $3 $4;
#OR
$hour = $1;
$minute = $2;
$seconds = $3;
$tod = $4;
print "Hour: $hour Minute:$minute Seconds:$seconds TOD:$tod";
#end


#!/usr/bin/perl -w
@array1 = ("might","night","right","tight","goodzight","dog");
foreach(@array1)
{
if(/[mnrt]ight/i)#character class
{
print $_\n;
}
if(/dog|cat|bird/i)#grops
{
print $_\n;
}
}
#end


#!/usr/bin/perl -w
$IN = "INFILE";
$filename = "data3";
open ($IN, "$filename") || die "Problems : $!";
while(<$IN>)
{
if(/^Mark/)
{
print $_;
}
if(m!^Mark!)#instead of / you can use m!
{
print $_;
}
if(/(.:)(.*\\)/)#c:\windows
{
print $_;#both values are not separated
print $1 $2;#both values are separated
}
if(/\+/)#get all line containing + sign. metacharacterss must be escaped
if(/yes/i)#get all lines containing yes word
if(/./)#removes all the lines
if(/[a-z]\d+/i)
if(/.*@.*/)#match email addresses
if(/(.*)@(.*)/)#match email addresses
if(/^$/) #match blank lines
}
#end

No comments:

Post a Comment