use grep to count the number of times a word got repeated in a file use grep to count the number of times a word got repeated in a file shell shell

use grep to count the number of times a word got repeated in a file


You may try something like:

grep -o "<item>" a.xml | wc -l


If you are just looking to count '< item>' alone, then I like MillaresRoo's grep -o solution. If you are looking to count items more generally, then consider:

$ sed 's/></>\n</g' a.xml | sort | uniq -c      1 <cause>      2 <item>      1 <queue>

Or, showing the input explicitly on the command line:

$ echo '<queue><item><cause><item>' | sed 's/></>\n</g' | sort | uniq -c      1 <cause>      2 <item>      1 <queue>


Using awk you can do that in a single command:

awk -F '<item>' '{print NF-1}' a.xml

Online Demo: http://ideone.com/vheDgq

OR to get total count for whole file use:

awk -F '<item>' '{s+=NF-1}END{print s}' a.xml