Grepping particular pattern from a log file
1) Extracting contents between the block P1 and P2
2) Filtering particular pattern from the extraction
Example:
P1
10,9:11/18013013582
,10:1
17,9:10/8013765028333384
17,9:10/801376502333884
17,9:10/801dd376502333884
,10:1
,11:1
,12:0
,167:0
289,525:0
308,578:0
P2
x=1
1,64:1
,70:H
P1
,580:2
17,9:10/80137650233
13,9:13/47202177053E0
,10:51
,487:5/G_CGI
26,27:0
,29:S
,31:0
P2
Need to Extract the values after the / --> content high lighted in blue, the script should also ignore the content between P2 and P1
Script
#!/usr/bin/perl
if(! defined($ARGV[0]))
{
print "USAGE: perl $0 'filename'\n";
exit;
}
open(FILE,"$ARGV[0]");
$je=0;
while($line=<FILE>)
{
if($line=~/P1/)
{
$x=1;
}
if(($x==1)&&($line=~/10,9|17,9/)||($line=~/P1|P2/))
{
@spl=split(/\//,$line);
push(@coll,$spl[1]);
}
if($line=~/P2/)
{
$x=0;
$y=start;
}
if($y eq "start")
{
print "####################################### BLOCK $je #################################################\n";
foreach (@coll)
{
if(defined($_))
{
chomp;
push(@coll1,$_);
}
}
for($i=0;$i<@coll1;$i++)
{
if($i%2==0)
{
$j=$i+1;
$coll1[$j]="null" if (!defined($coll1[$j]));
print $coll1[$i]." ".$coll1[$j]."\n";
}
}
print "##########################################################################################\n";
$y="stop";
@coll=' ';
@coll1=' ';
$je=$je+1;
}
}
close(FILE);
--------------------------------------------------------------------------------------------------------------------------------------
Results:
perl phaniperl sam
####################################### BLOCK 0 #################################################
18013013582 8013765028333384
801376502333884 801dd376502333884
##########################################################################################
####################################### BLOCK 1 #################################################
13233361170 28818773
##########################################################################################
####################################### BLOCK 2 #################################################
19079572509 6714829388
##########################################################################################
####################################### BLOCK 3 #################################################
19079572509 6714829388
##########################################################################################
####################################### BLOCK 4 #################################################
19079572509 4255912230
##########################################################################################
1) Extracting contents between the block P1 and P2
2) Filtering particular pattern from the extraction
Example:
P1
10,9:11/18013013582
,10:1
17,9:10/8013765028333384
17,9:10/801376502333884
17,9:10/801dd376502333884
,10:1
,11:1
,12:0
,167:0
289,525:0
308,578:0
P2
x=1
1,64:1
,70:H
P1
,580:2
17,9:10/80137650233
13,9:13/47202177053E0
,10:51
,487:5/G_CGI
26,27:0
,29:S
,31:0
P2
Need to Extract the values after the / --> content high lighted in blue, the script should also ignore the content between P2 and P1
Script
#!/usr/bin/perl
if(! defined($ARGV[0]))
{
print "USAGE: perl $0 'filename'\n";
exit;
}
open(FILE,"$ARGV[0]");
$je=0;
while($line=<FILE>)
{
if($line=~/P1/)
{
$x=1;
}
if(($x==1)&&($line=~/10,9|17,9/)||($line=~/P1|P2/))
{
@spl=split(/\//,$line);
push(@coll,$spl[1]);
}
if($line=~/P2/)
{
$x=0;
$y=start;
}
if($y eq "start")
{
print "####################################### BLOCK $je #################################################\n";
foreach (@coll)
{
if(defined($_))
{
chomp;
push(@coll1,$_);
}
}
for($i=0;$i<@coll1;$i++)
{
if($i%2==0)
{
$j=$i+1;
$coll1[$j]="null" if (!defined($coll1[$j]));
print $coll1[$i]." ".$coll1[$j]."\n";
}
}
print "##########################################################################################\n";
$y="stop";
@coll=' ';
@coll1=' ';
$je=$je+1;
}
}
close(FILE);
--------------------------------------------------------------------------------------------------------------------------------------
Results:
perl phaniperl sam
####################################### BLOCK 0 #################################################
18013013582 8013765028333384
801376502333884 801dd376502333884
##########################################################################################
####################################### BLOCK 1 #################################################
13233361170 28818773
##########################################################################################
####################################### BLOCK 2 #################################################
19079572509 6714829388
##########################################################################################
####################################### BLOCK 3 #################################################
19079572509 6714829388
##########################################################################################
####################################### BLOCK 4 #################################################
19079572509 4255912230
##########################################################################################
