wwe
khfgv
jfo
wwe
jhgfd
wwe
wwe
hoaha
hao
lkahe
wwe
I want to count the lines between the consecutive wwe's and store the count in variables. So for example the first variable value will be 2, (as there are two lines between the wwe's) second variable will be 1. The third variable value should be 0 and the fourth will be 3. I would prefer the awk solution and I guess using arrays will be better.
I started out with a simple statement:
awk '!/wwe/' file
but this does not give me missing lines (where there is no entry between the two consecutive wwe's.
my $flag =0 ;
my $cnt = 0;
while(<DATA>){
if(/wwe/){
if($flag eq 0){
$flag=1;
next;
}
print $cnt,"\n" if $flag eq 1;
$cnt=0;
}
else{
$cnt++;
}
}
__DATA__
wwe
khfgv
jfo
wwe
jhgfd
wwe
wwe
hoaha
hao
lkahe
wwe
Sorry I was not clear on the question. I may have multiple lines between the two patterns for example:
wwe
khfgv
jfo
mmm
nnn
wwe
jhgfd
wwe
wwe
hoaha
hao
lkahe
wwe
I was looking for a more general solution. Your script will print only the two lines, but if I want to print all the lines ONLY between the first two consecutive wwe's. In this case I should get:
khfgv
jfo
mmm
nnn
Are the two print statements for the two lines?How can I modify the script to print all lines between the two wwe's?