![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
||||
|
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
|
|
|||
|
Quote:
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. 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";
}
}
|
||||
| Google The UNIX and Linux Forums |