Count words/lines between two tags using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count words/lines between two tags using awk
# 1  
Old 02-17-2015
Count words/lines between two tags using awk

Is there an efficient
Code:
awk

that can count the number of lines that occur in between two tags.
For instance, consider the following text:

Code:
<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 that in between the first set of tags
Code:
<s>

and
Code:
</s>

there are 5 words (indicated by 5 lines) and in the second set of tags
Code:
<s>

and
Code:
</s>

there are 6 words (indicated by 6 lines).

The desired output would be as follows:

Code:
5
6

How can I write an
Code:
awk

that uses my
Code:
XML

tags as markers to count this information and obtain my desired output?
# 2  
Old 02-17-2015
Any attempts from your side?

---------- Post updated at 17:21 ---------- Previous update was at 17:16 ----------

Be it as it may... this one is not too daunting. Try
Code:
awk '/<s>/ {ST=NR} /<\/s>/{print NR-ST-1}' file
5
6

This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-17-2015
awk -f ow.awk myFile where ow.awk is:
Code:
$0 ~ "<s>",$0~"</s>" {c++}
$0 ~ "</s>" {print c-2;c=0}

This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 02-18-2015
Hello owwow14,

Following may also help you in same.
Code:
awk '/<s>/{getline;while($0 !~ /<\/s>/){A++;getline};print A;A=""}'  Input_file

Output will be as follows.
Code:
5
6

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 02-18-2015
Code:
$ awk -F"\n" -v RS="</s>\n" ' { print NR,NF-2 } ' file
1 5
2 6

This User Gave Thanks to anbu23 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. Shell Programming and Scripting

AWK count letters words

Hi All! can anyone help me with this code? I want to count words or letters in every line with if(count>20){else echo $myline} awk '/<script /{p=1} /<\/script>/{p=0; next}!p' index.html | while read myline; do echo $myline done Thank you !!! (3 Replies)
Discussion started by: sanantonio7777
3 Replies

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

5. Shell Programming and Scripting

Count lines AWK

Hi, how can I count the lines where a word appears in a file, using AWK? Example: file.txt: gold 1588 France gold 1478 Spain silver 1596 France emerald 1584 UK diamond 1478 Germany gold 1639 USA Number of lines where gold in text is = 3 I've try this, but all I get is the number... (3 Replies)
Discussion started by: Godie
3 Replies

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

7. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: zsudarshan
5 Replies

8. Shell Programming and Scripting

Swapping lines beginning with certain words using sed/awk

I have a large file which reads like this: fixed-address 192.168.6.6 { hardware ethernet 00:22:64:5b:db:b1; host X; } fixed-address 192.168.6.7 { hardware ethernet 00:22:64:5b:db:b3; host Y; } fixed-address 192.168.6.8 { hardware ethernet 00:22:64:5b:db:b4; host A; }... (4 Replies)
Discussion started by: ksk
4 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. UNIX for Dummies Questions & Answers

merging 2 lines with awk and stripping first two words

Hey all i am pretty new to awk... here my problem. My input is something like this: type: NSR client; name: pegasus; save set: /, /var, /part, /part/part2, /testpartition, /foo/bar,... (9 Replies)
Discussion started by: bazzed
9 Replies
Login or Register to Ask a Question