Sponsored Content
Top Forums Shell Programming and Scripting Grep for the same occurrence or maybe Sed Post 28058 by photon on Thursday 12th of September 2002 12:25:14 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
SVK::Log::Filter::Grep(3)				User Contributed Perl Documentation				 SVK::Log::Filter::Grep(3)

SYNOPSIS
SVK::Log::Filter::Grep - search log messages for a given pattern DESCRIPTION
The Grep filter requires a single Perl pattern (regular expression) as its argument. The pattern is then applied to the svn:log property of each revision it receives. If the pattern matches, the revision is allowed to continue down the pipeline. If the pattern fails to match, the pipeline immediately skips to the next revision. The pattern is applied with the /i modifier (case insensitivity). If you want case-sensitivity or other modifications to the behavior of your pattern, you must use the "(?imsx-imsx)" extended pattern (see "perldoc perlre" for details). For example, to search for log messages that match exactly the characters "foo" you might use svk log --filter "grep (?-i)foo" However, to search for "foo" without regards for case, one might try svk log --filter "grep foo" The result of any capturing parentheses inside the pattern are not available. If demand dictates, the Grep filter could be modified to place the captured value somewhere in the stash for other filters to access. If the pattern contains a pipe character ('|'), it must be escaped by preceding it with a '' character. Otherwise, the portion of the pattern after the pipe character is interpreted as the name of a log filter. STASH
/PROPERTY MODIFICATIONS Grep leaves all properties and the stash intact. perl v5.10.0 2008-08-04 SVK::Log::Filter::Grep(3)
All times are GMT -4. The time now is 05:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy