I need to find the 'number' of occurrences of particular words (C7STH, C7ST2C) that come in the output of a command. The command starts and ends with a 'fixed' text - START & END like below. This command is repeated many times against different nodes in the log file.
...
START
SLC ACL PARMG ST SDL SLI
0 A1 17 C7STH-1&&-31 MSC19-0/RTLTB2-385
1 A1 17 C7STH-65&&-95 MSC19-0/RTLTB2-1697
SLC ACL PARMG ST SDL SLI
0 A2 0 C7ST2C-4 ETRC18-0/RTLTB2-417
1 A2 0 C7ST2C-5 ETRC18-0/RTLTB2-449
2 A2 0 C7ST2C-6 ETRC18-0/RTLTB2-961
...
END
....
I am using flip-flop operator (if (/^START$/ .. /^END$/)to get each command output. Now
Is there a way to do 'grep' on this data without going line by line? Like can i get all the text between 'START' and 'END' into an array and do 'grep' on this etc?
Also is it 'ok' to have multiple levels of if blocks with flip-flop operator from performance point of view?
Maybe your looking for something along these lines:
#!/usr/bin/env perl
use strict;
use warnings;
my $word = q(stuff);
my @data;
while (<DATA>) {
if ( /^START/../^END/ ) {
chomp;
push @data, $_ unless /^(?:START|END)/;
}
if ( /^END/ ) {
my $str = "@data";
print +(scalar grep {/$word/} (split / /,$str)),
" occurances of '$word'\n";
@data = ();
}
}
__DATA__
this is a line
START of my stuff
more my stuff
and still more stuff
and lastly, yet more stuff
END of my stuff
this is another line
START again
stuff stuff stuff stuff
yet more stuff
END again
...which would output:
3 occurances of 'stuff'
5 occurances of 'stuff'