count number of lines between two tag


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count number of lines between two tag
# 1  
Old 09-03-2010
count number of lines between two tag

Hi
I have a big xml file of having many data
Code:
</Data>
</Node>
<Node>
List
<abc>3932590983
3396003964
3355713230</abc>
</Data>
</Node>
</Data>
</Node>
<abc>3932590983
3396003964</abc>

I want to count the lines present betweem tag starting with abc and ending with abc

so o/p will be like

Code:
abc=3
abc=2

Please help me to get data.
# 2  
Old 09-03-2010
Code:
$> awk '/abc/ && c==0 {c+=1; next} /abc/ && c>0 {c+=1; print "abc="c; c=0; next} c>0 {c+=1}' infile
abc=3
abc=2

# 3  
Old 09-03-2010
Hi thanks for your reply
Can we also print the line number of starting tag
like op

Code:
 
Line 5 abc=3
Line 11 abc=2

# 4  
Old 09-03-2010
Yes we can:
Code:
$> awk '/abc/ && c==0 {c+=1; l=NR; next} /abc/ && c>0 {c+=1; print "Line",l,"abc="c; c=0; next} c>0 {c+=1}' infile
Line 5 abc=3
Line 12 abc=2

# 5  
Old 09-03-2010
Another one:
Code:
awk '/<abc>/{c=0;n=NR}{c++} /<\/abc>/{print "Line " n " abc=" c}" file

# 6  
Old 09-03-2010
Quote:
Originally Posted by zaxxon
Yes we can:
Code:
$> awk '/abc/ && c==0 {c+=1; l=NR; next} /abc/ && c>0 {c+=1; print "Line",l,"abc="c; c=0; next} c>0 {c+=1}' infile
Line 5 abc=3
Line 12 abc=2


Can you please Explain the above code in detail for me?
# 7  
Old 09-03-2010
Hi I am not getting correct output
My actual file is like

Code:
 
<?xml version="3.0" encoding="ISO-8799-1" standalone="yes"?>
<Config File="trees/banking/saved/data.xml" Origin="Member1">
<AccountStructure Name="Bank_acc" type="account" id="76" ServiceId="account" TariffType="account">
<Index>0</Index>
<Comment>Banking account
account class : 5692 ,2122,
2345 , 4567
PLAN STRATED FROM 20100401.</Comment>
<DefaultPrice>12.0</DefaultPrice>
<DefaultInterval>12</DefaultInterval>
<Node>
account
<Node>
Saving
<Condition id="2">
ServiceCode
<Code>0</Code>
</Condition>
<Condition id="1">
AccountCase
<Case>0</Case>
</Condition>
<Node>
Home
<Condition>
abcList
<Type>3</Type>
<abc>0
1
2
3
5
6
7
8
9</abc>
</Condition>
<Node>
Offnet
<Node>
within bank
<Node>
within
<Condition>
abcList
<Type>0</Type>
<abc>0
1
2
3
4
5
6
9</abc>
</Condition>

When I am running command I am getting

Code:
 
awk '/abc/ && c==0 {c+=1; l=NR; next} /abc/ && c>0 {c+=1; print "Line",l,"abc="c; c=0; next} c>0 {c+=1}' ta
Line 26 abc=3
Line 36 abc=10
Line 47 abc=8

wheras right op should be like

Code:
Line 31 abc=9
Line 50 abc=8

Please help me

---------- Post updated at 11:17 PM ---------- Previous update was at 11:10 PM ----------

Hi All
Thanks for reply
this code worked perfectly

Code:
awk '/<abc>/{c=0;n=NR}{c++} /<\/abc>/{print "Line " n " abc=" c}' file

Thanks again

Last edited by radoulov; 09-03-2010 at 05:15 AM.. Reason: Added code tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To search for a particular tag in xml and collate all similar tag values and display them count

I want to basically do the below thing. Suppose there is a tag called object1. I want to display an output for all similar tag values under heading of Object 1 and the count of the xmls. Please help File: <xml><object1>house</object1><object2>child</object2>... (9 Replies)
Discussion started by: srkmish
9 Replies

2. Shell Programming and Scripting

Compare two files and count number of matching lines

Dear All, I would like to compare two files and return the number of matches found. Example File A Lx2 L1_Mus1 L1Md_T Lx5 L1M2 L1_Mus3 Lx3_Mus Lx9 Lx2A L1Md_A L1Md_F2 File B L1_Mus3 L1_Mus3 (3 Replies)
Discussion started by: paolo.kunder
3 Replies

3. Shell Programming and Scripting

Want to count the number of lines after the first line

hi, How can i count the number of lines after the first line in a flat file in unix? Say i have a flat file with a header like: Student Name Student ID .... Tnx (7 Replies)
Discussion started by: reignangel2003
7 Replies

4. UNIX for Dummies Questions & Answers

Comparing two files and count number of lines that match

Hello all, I always found help for my problems using the search option, but this time my request is too specific. I have two files that I want to compare. File1 is the index and File2 contains the data: File1: chr1 protein_coding exon 500 600 . + . gene_id "20532";... (0 Replies)
Discussion started by: DerSeb
0 Replies

5. Shell Programming and Scripting

Count the number of lines in a file with one condition

Hi Everyone, 1.txt Mon 08 Feb 2010 12:30:44 AM MYT;1265560244;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:46 AM MYT;1265560246;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:48 AM MYT;1265560248;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:50 AM... (1 Reply)
Discussion started by: jimmy_y
1 Replies

6. Shell Programming and Scripting

count the number of lines that start with the number

I have a file with contents similar to this. abcd 1234 4567 7666 jdjdjd 89289 9382 92 jksdj 9823 298 I want to write a shell script which count the number of lines that start with the number (disregard the lines starting with alphabets) (1 Reply)
Discussion started by: grajp002
1 Replies

7. UNIX for Dummies Questions & Answers

Count of Number of Lines in a File

Dear Members, I want to count the number of lines in a file; for that i am using the following command : FILE_LINE_COUNT=`wc -l $INT_IN/$RAW_FILE_NAME` if i do an echo on FILE_LINE_COUNT then i get 241 /home/data/testfile.txt I don't want the directory path to be displayed. Variable... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

8. Programming

to count the number of commented lines in a file

can someone guide me how to have a C pgm to count the number of commented lines? (3 Replies)
Discussion started by: naan
3 Replies

9. Shell Programming and Scripting

Count number of lines in Binary file

Hi Friends Please help me out to count number of lines in binary file. It gives some wrong(less) using wc -l. Is there any other way to count lines of binary file. Thanks. (3 Replies)
Discussion started by: vanand420
3 Replies

10. UNIX for Dummies Questions & Answers

word count showing wrong number of lines

Hi , I am using SUN OS Version 5.6. I have a file that contains records of length 270. when I do 'set nu' in vi editor, I get the count as 86. whereas when I do "wc -l" on the command prompt, it shows the count as only 85. this is very strange. why would the 'wc' show 1 record less. The job... (3 Replies)
Discussion started by: tselvanin
3 Replies
Login or Register to Ask a Question