Counting lines between two patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting lines between two patterns
# 1  
Old 02-01-2010
Counting lines between two patterns

Hi Guys,

I have a file as follows:

Code:
 
wwe
khfgv
jfo
wwe
jhgfd
wwe
wwe
hoaha
hao
lkahe
wwe

I want to count the lines between the consecutive wwe's and store the count in variables. So for example the first variable value will be 2, (as there are two lines between the wwe's) second variable will be 1. The third variable value should be 0 and the fourth will be 3. I would prefer the awk solution and I guess using arrays will be better.

I started out with a simple statement:

Code:
awk '!/wwe/' file

but this does not give me missing lines (where there is no entry between the two consecutive wwe's.

Thanks in advance for your help.
# 2  
Old 02-01-2010
Code:
awk ' /wwe/ { if ( f!="") {print f-1}; f=0;f1=1 } f1 { f++ } ' file

# 3  
Old 02-01-2010
Thanks, that worked.
# 4  
Old 02-01-2010
Code:
awk '/wwe/{if(p)print NR-p-1;p=NR}' infile

Code:
2
1
0
3

# 5  
Old 02-02-2010
Code:
my $flag =0 ;
my $cnt = 0;
while(<DATA>){
	if(/wwe/){
		if($flag eq 0){
			$flag=1;
			next;
		}
		print $cnt,"\n" if $flag eq 1;
		$cnt=0;
	}
	else{
		$cnt++;
	}
}
__DATA__
wwe
khfgv
jfo
wwe
jhgfd
wwe
wwe
hoaha
hao
lkahe
wwe

# 6  
Old 02-02-2010
Thanks,summer_cherry.

Now I want to print the lines between only the first two consecutive "wwe" lines. So my file output should be:

Code:
khfgv
jfo

How can I do this?

I tried using:
Code:
awk '! /wwe/,/wwe/' file

but that gives me
Code:
khfgv
jfo
wwe
jhgfd
wwe
hoaha
hao
lkahe
wwe

# 7  
Old 02-02-2010
Quote:
Originally Posted by npatwardhan
Thanks,summer_cherry.

Now I want to print the lines between only the first two consecutive "wwe" lines. So my file output should be:

Code:
khfgv
jfo

How can I do this?

I tried using:
Code:
awk '! /wwe/,/wwe/' file

but that gives me
Code:
khfgv
jfo
wwe
jhgfd
wwe
hoaha
hao
lkahe
wwe

Code:
awk '/wwe/ && ++c <= 2 {getline; print}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

3. UNIX for Dummies Questions & Answers

Counting # of lines

Counting number of lines: sp I am trying to figure out a script to count the number of text files in cywig and have it give me a number (as the answer) any help would be appreciated. I am new here, so be gentle :D (3 Replies)
Discussion started by: unicksjp
3 Replies

4. Shell Programming and Scripting

sed counting lines

Hi, I'm using the command: sed -n '$=' $1 on a sh script on AIX. This script is used to count the number of lines of files. If the file has no lines at all the command doesn't return nothing. I need the command to return 0 when the file has no lines at all. How can I achieve this? Best... (5 Replies)
Discussion started by: jppedroso
5 Replies

5. Shell Programming and Scripting

Counting lines for each application

Hi All, I have a output that suppose to be like this (see below please) App : Line counts === ================== AAA: 100 BBB: 201 CCC: 137 DDD: 32 EEE: 55 for i in `ps -ef | grep App`; do print $i; done This only shows App : === (12 Replies)
Discussion started by: Beginer0705
12 Replies

6. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

7. Shell Programming and Scripting

counting the number of lines - again

Hi all, I use bash shell and I have a problem with wc. I would like to determine the number of lines in a file so I do wc -l filename but I don't want to get the filename again I just would like to have the number of lines and use it in a variable. Can anybody help? Thank you, (7 Replies)
Discussion started by: f_o_555
7 Replies

8. Shell Programming and Scripting

displaying/ counting lines

I have a file called xx with the env redirected into it 5 times: env >> xx env >> xx env >> xx env >> xx env >> xx I have to read an input file (here: xx) and look for occurrences of the current user who is executing this script. Once finding an occurrence of the username I have to take that... (2 Replies)
Discussion started by: aga
2 Replies

9. UNIX for Dummies Questions & Answers

displaying/ counting lines

I have a file called xx with the env redirected into it 5 times: env >> xx env >> xx env >> xx env >> xx env >> xx I have to read an input file (here: xx) and look for occurrences of the current user who is executing this script. Once finding an occurrence of the username I have to take that... (4 Replies)
Discussion started by: aga
4 Replies

10. UNIX for Dummies Questions & Answers

Counting patterns in a shell string

Hello, I am writing a shell script and I need to find a way to count the number of whitespaces in a string. Eg: NAME="Bob Hope" I am looking for a way to count the number of whitespaces in this string. So a command that would take this string and return 1. Or take "First Middle Last"... (3 Replies)
Discussion started by: kevin80
3 Replies
Login or Register to Ask a Question