Counting lines between two patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting lines between two patterns
# 8  
Old 02-02-2010
That is giving me this output:

Code:
khfgv
jhgfd

I want:

Code:
khfgv
jfo

# 9  
Old 02-02-2010
Sorry, I misread the question. It should be:
Code:
awk '/wwe/{getline; print; getline; print; exit}' file

# 10  
Old 02-02-2010
Code:
awk '/wwe/{p++;next}p<2' infile

# 11  
Old 02-02-2010
Sorry I was not clear on the question. I may have multiple lines between the two patterns for example:

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

I was looking for a more general solution. Your script will print only the two lines, but if I want to print all the lines ONLY between the first two consecutive wwe's. In this case I should get:

Code:
khfgv
jfo
mmm
nnn

Are the two print statements for the two lines?How can I modify the script to print all lines between the two wwe's?

Thanks for your patience on this.
# 12  
Old 02-02-2010
To handle a variable number of records:

Code:
awk '/wwe/&&_++{exit}!/wwe/' infile

# 13  
Old 02-02-2010
Thanks, radulov. It worked.

One last question: how can you modify the script if wwe is not the first word on the line; in other words, if my file was like this:

Code:
sss wwe
kkk khfgv
aaa jfo
qqq mmm
kjgfd nnn
rrr wwe
ttt jhgfd
uuu wwe
ooo wwe
ppp hoaha
ddd hao
fff lkahe
hhh wwe

and I want:

Code:
kkk khfgv
aaa jfo
qqq mmm
kjgfd nnn

Should I use $2 = in your awk script? Thanks again for your help.

Edit: I tried it and it worked. Sorry for the confusion.
# 14  
Old 02-02-2010
No modification should be needed ...
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