Grep for the same occurrence or maybe Sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep for the same occurrence or maybe Sed
# 1  
Old 08-29-2002
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
# 2  
Old 08-29-2002
How about ksh?
Code:
#!/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

# 3  
Old 09-11-2002
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!

Code:
#!/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

# 4  
Old 09-11-2002
Quote:
Originally posted by photon
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!

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
# 5  
Old 09-12-2002
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.


Code:
#!/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. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep util last occurrence

I have file contents /tmp/x/abc.txt /home/bin/backup/sys/a.log I need this output: /tmp/x/ /home/bin/backup/sys/ Can somebody please help me out Please use CODE tags as required by forum rules! (3 Replies)
Discussion started by: jhonnyrip
3 Replies

2. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

3. UNIX for Dummies Questions & Answers

Substitution in first occurrence with sed

I have the following script: sed '/string1/,/string2/!d' infile I want to apply the script to the first occurrence only. I have tried sed '0,/string1/,/string2/!d' infile Of course, that does not work Any help will be greatly appreciated (12 Replies)
Discussion started by: Xterra
12 Replies

4. UNIX for Dummies Questions & Answers

Sed, last occurrence

How to find last occurrence of a keyword in a file using sed. (4 Replies)
Discussion started by: nexional
4 Replies

5. Shell Programming and Scripting

Modifying sed to only change last occurrence.

I'm using sed to switch integers (one or more digits) to the other side of the ':' colon. For example: "47593:23421" would then be "23421:47593". The way it functions right now, it is messing my settings file to use with gnuplot. The current command is: sed 's/\(*\):\(*\)/\2:\1/' out3 >... (3 Replies)
Discussion started by: D2K
3 Replies

6. UNIX Desktop Questions & Answers

grep a specific amount of occurrence

hey , i m trying to figure out how to do the following : i got a text file the looks like so: 1031 1031 1031 1031 1031 1031 1031 1031 16500 16500 16500 16500 1031 1031 (4 Replies)
Discussion started by: boaz733
4 Replies

7. UNIX for Dummies Questions & Answers

grep first occurrence but continue to next entry in patternfile

I have 1300 files (SearchFiles0001.txt, SearchFiles0002.txt, etc.) , each with 650,000 lines, tab-delimited data. I have a pattern file, with about 1000 lines with a single word. Each single word is found in the 1300 files once. If I grep -f PatternFile.txt SearchFiles*.txt >OutputFile.txt... (2 Replies)
Discussion started by: newhavendweeb
2 Replies

8. UNIX for Dummies Questions & Answers

Line number of an occurrence using grep

Hi All, is there a way to extract the line number of an occurrence using grep? I know that with the -n option it prints out the line number as well. I would like to assign the line number to a variable. Thanks, Sarah (5 Replies)
Discussion started by: f_o_555
5 Replies

9. UNIX for Dummies Questions & Answers

grep usage - exit after first occurrence

Hi, Is there any way of using grep (this may be done in awk, not sure?) that I can stop grep'n a file once I have found the first occurrence of my search string. Looking through grep man pages -q will exit without printing the lines after the first match, but I need the output. I have... (5 Replies)
Discussion started by: nhatch
5 Replies

10. Shell Programming and Scripting

Using sed to substitute first occurrence

I am trying to get rid of some ending tags but I run into some problems. Ex. How are you?</EndTag><Begin>It is fine.</Begin><New> Just about I am trying to get rid of the ending tags, starts with </ and ending with >. (which is </EndTag> and </Begin>) I tried the following sed... (2 Replies)
Discussion started by: quixoticking11
2 Replies
Login or Register to Ask a Question