|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 ![]() |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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 |
|
#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
|
|
#5
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Problem with GREP |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|