total occ.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting total occ.
# 1  
Old 05-09-2008
total occ.

hi all,


I want to find out the total no. of occurence of <song_id> and </song_id>
seperately from a xml file.


Any Idea.


Regards,
gander_ss
# 2  
Old 05-09-2008
Hi,

you can use grep with -c option

grep -c '<song_id>' filename

grep -c '</song_id>' filename

Thanks
Penchal
# 3  
Old 05-09-2008
Code:
grep -c "\<song\>" <filename>

grep -c "\<\\song\>" <filename>

# 4  
Old 05-09-2008
Quote:
Originally Posted by aju_kup
Code:
grep -c "\<song\>" <filename>

grep -c "\<\\song\>" <filename>

grep -c will give count of lines containing the pattern not the pattern count.
# 5  
Old 05-09-2008
Bug

You can use awk to find the pattern count provided words are separated by space or tabs.

like
awk ' BEGIN{ count=0 }{
for (i=1; i<=NF ;i++){
if ( $i == "<song_id>") count++; }
}END{ print count }'

SmilieSmilie
# 6  
Old 05-09-2008
Hi,

This also might be useful

awk '{ x=gsub(/<song_id>/,yyy,$0)
total1+=x
y=gsub(/<\/song_id>/,xxx,$0)
total2+=y }
END { print "Occurences of <song_id> is ",total1,"\n","Occurences of </song_id> is ",total2}' filename



Thanks
Penchal
# 7  
Old 05-09-2008
using Perl:
Code:
#!/usr/local/bin/perl
# count_song_tags.pl
my $filename = shift;
my $count    = 0;
open (X_FILE, '<', $filename)  or  die "Failed to read file $filename : $!";
{
    local $/;
    while (<X_FILE>) {
        while (m#<song_id>(.*?)</song_id>#gs) {
            $count++;
        }
    }
}
close (X_FILE);
print "$count song_id tag(s) found.\n";

run this script as:
Code:
perl count_song_tags.pl filename.xml

Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Solaris

Total

i want to list Total HDD count in any soalris machine..can someone suggest some commands or combinations of commands (6 Replies)
Discussion started by: omkar.jadhav
6 Replies

2. Shell Programming and Scripting

Help with sum total number of record and total number of record problem asking

Input file SFSQW 5192.56 HNRNPK 611.486 QEQW 1202.15 ASDR 568.627 QWET 6382.11 SFSQW 4386.3 HNRNPK 100 SFSQW 500 Desired output file SFSQW 10078.86 3 QWET 6382.11 1 QEQW 1202.15 1 HNRNPK 711.49 2 ASDR 568.63 1 The way I tried: (2 Replies)
Discussion started by: patrick87
2 Replies

3. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

4. IP Networking

Total byte

Hi I've a pkts trace and I'm performing some test on it. I'd like to figure out also the numbers of total byte in that trace. Any idea? thanks in advance D. (0 Replies)
Discussion started by: Dedalus
0 Replies

5. UNIX for Dummies Questions & Answers

du total

Hi All Can anyone help me with the following du querry. I am trying to achieve a total size for all the zipped files in a directory. Using du -k *.gz gets me a file by file list but no handy total at the bottom. Thanks Ed (9 Replies)
Discussion started by: C3000
9 Replies

6. UNIX for Dummies Questions & Answers

grep running total/ final total across multiple files

Ok, another fun hiccup in my UNIX learning curve. I am trying to count the number of occurrences of an IP address across multiple files named example.hits. I can extract the number of occurrences from the files individually but when you use grep -c with multiple files you get the output similar to... (5 Replies)
Discussion started by: MrAd
5 Replies
Login or Register to Ask a Question