Count the no of lines between two words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count the no of lines between two words
# 1  
Old 09-17-2009
Java Count the no of lines between two words

Please help in the following problem:
Input is:
Pritam
123
456
Patil
myname
youname
Pritam
myproject
thisproject
iclic
Patil
remaining text
some more text

I need the command which will display the no of lines between two words in the whole file.
e.g. Display all the no of lines between 'Pritam' and 'Patil'.

so out put should be 2, 3. (there r 2 lines between two words, subsequently there r 3 lines)

Thanks for any help in advance.

# 2  
Old 09-17-2009
Code:
 
awk '/Pritam/,/Patil/{c++;next}{if(c!=0) print c-2;c=0}' infile

# 3  
Old 09-17-2009
Can be done following way...
Code:
awk '/Pritam/,/Patil/{n++}; END {print n-2}' filename

# 4  
Old 09-17-2009
Quote:
Originally Posted by ganesh.mandlik
Can be done following way...
Code:
awk '/Pritam/,/Patil/{n++}; END {print n-2}' filename

I tried your code and it gave me 7.
But the OP is expecting output 2 and 3.
# 5  
Old 09-17-2009
Try this

Code:
awk '/Pritam/,/Patil/ {c++;} /Patil/ {print c-2;c=0}' file

Regards,

Ranjith
# 6  
Old 09-17-2009
something like this :

Code:
awk '/Pritam/ {c++;next} /Patil/ {print a[c];next} {a[c]++} ' input_file.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Count unique words

Dear all, I would like to know how to list and count unique words in thousands number of text files. Please help me out thanks in advance (9 Replies)
Discussion started by: imranrasheedamu
9 Replies

2. Shell Programming and Scripting

Count the words starting with 3-

OS : Oracle Linux 6.5 Shell : bash I have a file whose contents look like below. I want to count the number of occurences of strings starting with 3-. How can I do this ? I couldn't wordwrap the below line. Hence it looks long. '3-90892405251', '3-90892911050', '3-90893144163',... (8 Replies)
Discussion started by: John K
8 Replies

3. Shell Programming and Scripting

Count words/lines between two tags using awk

Is there an efficient awk that can count the number of lines that occur in between two tags. For instance, consider the following text: <s> Hi PP - my VBD - name DT - is NN - . SENT . </s> <s> Her PP - name VBD - is DT - the NN - same WRT - . SENT - </s> I am interested to know... (4 Replies)
Discussion started by: owwow14
4 Replies

4. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

5. Shell Programming and Scripting

Count lines and words of a stream output with tail

Hello, I need to tail -f a file output stream and I need to get only lines that contains "get" and "point" in the same line. It doesn't matter the order. Then I need only the text BEFORE "point". I have to count each line and perform other serveral actions after this has performed 3 times.... (9 Replies)
Discussion started by: Kibou
9 Replies

6. Shell Programming and Scripting

Scripting help to identify words count in lines

Hi everybody, i have this biological situation to fix: > Id.1 ACGTACANNNNNNNNNNNACGTGCNNNNNNNACTGTGGT >Id.2 ACGGGT >Id.3 ACGTNNNNNNNNNNNNACTGGGGG >Id.4 ACGTGCGNNNNNNNNGGTCANNNNNNNNCGTGCAAANNNNN ........ .... These are nucleotidic sequences with some "NNNN..." always of the same... (4 Replies)
Discussion started by: Giorgio C
4 Replies

7. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

8. Shell Programming and Scripting

Count words

Hi, does anyone know the command to count words by its length. I need to Count the number of five letter words that I have in a file with thousand of words. thanks (3 Replies)
Discussion started by: fabioamaury
3 Replies

9. Shell Programming and Scripting

awk help needed in trying to count lines,words and characters

Hello, i am trying to write a script file in awk which yields me the number of lines,characters and words, i checked it many many times but i am not able to find any mistake in it. Please tell me where i went wrong. BEGIN{ print "Filename Lines Words Chars\n" } { filename=filename + 1... (2 Replies)
Discussion started by: salman4u
2 Replies

10. Shell Programming and Scripting

count no of words in a line

hi i have a string like str=abc def ghi jkl now i want to count the no of words in the string please help (7 Replies)
Discussion started by: satish@123
7 Replies
Login or Register to Ask a Question