Grep for the same occurrence or maybe Sed

Hi, I have a file that looks like this

dasdjasdjoasjdoasjdoa SYN dakspodkapsdka
asdasdasdasdasdasdasd SYN sdfsdfsdfsdfdf
shfishifhsdifhsidhfif fsdfsdfsdfsdfs
sdfsdfsdfsdsdfsdfsdff cercercercerce
sdasdajsdoajsodasodoo FIN dasdaskdpasdda
dkaspdkaskdpaskpaskdp FIN asdasdasdasdas

dasdjasdjoasjdoasjdoa SYN dakspodkapsdka
asdasdasdasdasdasdasd SYN sdfsdfsdfsdfdf
shfishifhsdifhsidhfif fsdfsdfsdfsdfs
sdfsdfsdfsdsdfsdfsdff cercercercerce
sdasdajsdoajsodasodoo FIN dasdaskdpasdda
dkaspdkaskdpaskpaskdp FIN asdasdasdasdas

The file is a huge log file with always 2 lines with SYN in exactly the same place one after the other, and also 2 FINS together. We have discovered that we are getting errors where 3 SYN lines are together, and I want to identify where these occurences are ie

dasdjasdjoasjdoasjdoa SYN dakspodkapsdka
asdasdasdasdasdasdasd SYN sdfsdfsdfsdfdf
asdasdasdasdasdasdasd SYN sdfsdfsdfsdfdf
shfishifhsdifhsidhfif fsdfsdfsdfsdfs
sdfsdfsdfsdsdfsdfsdff cercercercerce
sdasdajsdoajsodasodoo FIN dasdaskdpasdda
dkaspdkaskdpaskpaskdp FIN asdasdasdasdas

Ive looked at using grep to search for '3 SYNS' but i cant seem to get it to work.

Does anybody know how i can search for an occurrence of SYN in the same place , three lines running

Any help would be greatly appreciated

How about ksh?

#!/usr/bin/ksh
IFS=""
read line1
read line2
while read line3 ; do
        if [[ $line1 = *SYN* && $line2 = *SYN* &&
                                $line3 = *SYN* ]] ; then
               echo "$line1"
               echo "$line2"
               echo "$line3"
        fi
        line1="$line2"
        line2="$line3"
done
exit 0

grep will not do it, because you need to store values.

Try Perl.

This code should print output such as.

Line: 344 Content: dasdjasdjoasjdoasjdoa SYN dakspodkapsdka
Line: 345 Content:asdasdasdasdasdasdasd SYN sdfsdfsdfsdfdf
Line: 346 Content:asdasdasdasdasdasdasd SYN sdfsdfsdfsdfdf
Dude, you have 3 lines!

#!/usr/local/bin/perl
#By Photon
#

$file = 'data.txt' ;        
open(INFO, "<$file" ) ;               
@lines = <INFO> ;                     
close(INFO) ;                         

print "<HTML> <HEAD> <TITLE> PERL output </TITLE> </HEAD>\n" ;
print "  <BODY>\n" ;

$count = 0;
foreach $line (@lines)               
{                                     
	if ($line =~ /SYN/){
		if ($count < 3) {
   			# print line number and formatted lines to screen
   			$count++;
   			print "\n   <P>Line: $count Content: $line   </P>" ;
		}
		elsif($count == 3){
			print "\n   <P>Line: $count Content: $line   </P>" ;
			print "\n	<P>Dude, you have 3 lines!</p>
			$count = 0;
		}
		else{#this will not hapen
		}
	}else{ #reset count
		$count = 0;
	}
}

print "\n  </BODY>\n</HTML>\n" ;


# DONE

just a couple of comments on the code if you dont mind.

1) dont use html. (unless you want to display it on a web page. in that case i would opt to make it a cgi and be able to run it on demand via the web.
2) the code as is does not work. your missing a double quote and a semicolon.
3) you will only get lines 1,2,3 not 4,5,6 and so on down the file. just an FYI. remember your resetting $count almost everytime.
4) instead of $count == 3 its prolly better to do a catch and and say ($count => 3) you never know if there can be more then 3.
5) remove the first print. so only the >=3 SYN lines print.

other wise pretty cool man. niCe job.

*Perderabo is my ksh hero

You are absolutely correct Optimus_P. I did
not even run my program. I did it slopy stile
when I was at work.

Here is a similar script, that runs.

#!/usr/local/bin/perl
#By Photon
#

$file = 'data.txt' ;        
open(INFO, "$file" ) ;               
@lines = <INFO> ;                     
close(INFO) ;                         


$count = 0;
$num_line = 0;
foreach $line (@lines){

	# Count the number of lines
	$num_line++; 
	    
	# Count consecutive lines with SYN                                    
	if ($line =~ m/SYN/){
		#print "$num_line\n";
                $count++;
        }else{ # Reset count
        	$count=0;
        }
        
        # print out results of lines
        if ($count >= 3){
        	print "You have three or more consecutive lines at:\n";
        	print "\tLines : ";
        	$j = $num_line;
        	for ( $i = $count ; $i > 0 ; $i-- ) {
        		print "$j  ";
        		$j--;
        	}
        	print "\n";
        }
 
}

There is always room for improvement. :slight_smile: