Friday, 31 May 2013

Greping particular word , along with word behind that word in perl

open(FILE,"sam");
while($line=<FILE>)
{
@s=split(":",$line);
for($i=0;$i<@s;$i++)
{
if($s[$i]=~/\(Agreement/)
{
$k=$s[$i+1];
print $s[1]."\t".$s[2]."\t".$s[$i]."\t".$k."\n";
}
}
}
close(FILE);

Thursday, 23 May 2013

storing two or more output in same variable in perl

open(FILE,"/etc/passwd");
while($line=<FILE>)
{
if($line=~/sshd|sam/)
{
$r.=$line;
}
}
close(FILE);
print $r;

Sunday, 19 May 2013

Grepping a particular range of word in a file using perl script

USAGE: perl filename $startingword $lastword $filename

use warnings;
$file=$ARGV[2];
open(FILE,"$file");
$first=$ARGV[0];
$second=$ARGV[1];
while($line=<FILE>)
{
$e++;
if($line=~/$first/)
{
$f=$e;
}
elsif($line=~/$second/)
{
$s=$e;
}
}
close(FILE);
open(FILE1,"$file");
while(<FILE1>)
{
$w++;
if(($w>$f)&&($w<$s))
{
print $_."\n";
}
}
close(FILE1);

Friday, 17 May 2013

Perl multiple pattern match

@q=
(
qr/^luci\b/,
qr/sss/,
);
open(FILE,"sam");
while($line=<FILE>)
{
for($i=0;$i<@q;$i++)
{
if($line=~/$q[$i]/)
{
print "match pattern $line.\n";
}
}
}
close(FILE);

Greping exact word in a file using perl script ( Border usage)

USAGE: perl "script file" "word to grep" "file name where you going to perform the action"

#!/usr/bin/perl
$word=$ARGV[0];
$file=$ARGV[1];
open(FILE,"$file");
while($line=<FILE>)
{
if($line=~/\b$word\b/ )
{
print $line."\n";
}
}
close(FILE);

Thursday, 16 May 2013

find and replace using perl script

USAGE: perl "scriptname" "word to find" "word to replace" "file to which you are going to perform the  action"

$find=$ARGV[0];
$replace=$ARGV[1];
$file=  $ARGV[2];
open(FILE,"$file");
while($line=<FILE>)
{
if($line=~/bash/)
{
$line=~s/$find/$replace/;
print $line."\n";
}
else
{
print $line."\n";
}
}
close(FILE);

Reading each word in a file and counting no of words using perl script

open(FILE,"/etc/passwd");
while($line=<FILE>)
{
@s=split(":",$line);
$r++;
push(@e,@s);
}
foreach $i (@e)
{
print $i."\n";
$w++;
}
print "no of words: $w.\n";
print "No of lines: $r.\n";
close(FILE);

Wednesday, 15 May 2013

Tuesday, 14 May 2013

greping the file with limit

use warnings;
$limit1=$ARGV[0];
$limit2=$ARGV[1];
$file1=$ARGV[2];
open(FILE,"$file1");
sub sam
{
$i=$_[0];
while(<$i>)
{
if(/$limit1/ .. /$limit2/)
{
print $_;
}
}
}
&sam(FILE);

Sunday, 5 May 2013

Grep command in perl

$line=$ARGV[1]||<STDIN>;
$gre=$ARGV[0];
open(FILE,"$line");
while($line1=<FILE>)
{
if ( $line1 =~ /$gre/ )
{
print $line1."\n";
}
}
close(FILE);