The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Getting the lines between last occurrence of two patterns rvz Shell Programming and Scripting 8 08-08-2008 02:32 PM
SED replace string by occurrence uttamhoode Shell Programming and Scripting 4 03-05-2008 01:04 AM
Replace second occurrence only lyoncc Shell Programming and Scripting 5 12-26-2007 07:21 PM
grep usage - exit after first occurrence nhatch UNIX for Dummies Questions & Answers 4 11-30-2007 03:21 AM
awk + last occurrence agibbs UNIX for Dummies Questions & Answers 2 10-06-2007 12:32 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-29-2002
Registered User
 

Join Date: Mar 2002
Posts: 170
Stumble this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-29-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,430
Stumble this Post!
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
Reply With Quote
  #3 (permalink)  
Old 09-11-2002
photon's Avatar
Registered User
 

Join Date: Jul 2002
Posts: 142
Stumble this Post!
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
Reply With Quote
  #4 (permalink)  
Old 09-11-2002
flim flam flamma jamma
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
Stumble this Post!
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
Reply With Quote
  #5 (permalink)  
Old 09-11-2002
photon's Avatar
Registered User
 

Join Date: Jul 2002
Posts: 142
Stumble this Post!
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:06 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0