SunQuest
           UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old March 6th, 2008, 11:04 AM
Poindexter Poindexter is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 6 Poindexter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 19 m 38 sec
Reputation Power: 0
Problem with GREP

Hello,

I am having a small problem with using the unix command grep on an XML file.

I need to count the number of occurrences of a certain tag in the XML file.

I'm using this command but the count is 36 and the count should be only 9, any ideas?

Code:
grep "<tag1>" myfile.xml | wc -w



Reply With Quote
  #2  
Old March 6th, 2008, 09:22 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 591 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 14 h 1 m 6 sec
Reputation Power: 100
Send a message via AIM to L7Sqr
without seeing your output this is a stab in the dark, but maybe using the -l flag to wc instead of -w
Code:
 grep "<tag>" file.xml | wc -l
__________________
-- I'll provide you with reference points; if they dont work, refer to something else.

If you process text, this might make your life a little easier.

Reply With Quote
  #3  
Old March 7th, 2008, 02:43 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2006
Posts: 671 SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 23 h 26 m 49 sec
Reputation Power: 196
Ouch!

Ok, the grep will only copy out the lines with the <tag1> in, then you are counting all words in all those lines.

You could use grep -c "<tag1>" but that would only count the number of lines with that tag so if you had the tag twice on a line it'd only be counted once.
__________________
"I feel so miserable without you; it's almost like having you here" - Stephen Bishop

Reply With Quote
  #4  
Old May 2nd, 2008, 02:49 PM
krisrmgua krisrmgua is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 64 krisrmgua User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 36 m 37 sec
Reputation Power: 4
Theres a bash script i found someone wrote that might do the trick. http://www.unix.com/linux/45575-find-multiple-strings-count-file.html

Reply With Quote
  #5  
Old May 22nd, 2008, 03:22 PM
ShaqDiesel ShaqDiesel is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 34 ShaqDiesel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 18 m 16 sec
Reputation Power: 2
First, I think you need to escape the "<" and ">" or surround the tag in single quotes.

Second, since grep does counts of lines, just using grep isn't going to give you a count of the number of times this tag exists in your file unless you know it can only occur at most once on each line. I would suggest:

1.Shell script where you go through each line and do:
$count = 0;
while($line != EOF)
{
$count+=awk -F\<tag\> print {NF};
}

2.Or, in perl you can use regex with the g option and store that into an array, then store that into a scalar which gives you the number of occurences per line to add onto your $count.
source: http://www.perlmonks.org/?node_id=410702

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Problem with GREP


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway