LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Word count with grep (https://www.linuxquestions.org/questions/linux-software-2/word-count-with-grep-414971/)

DiagonalArg 02-13-2006 11:53 AM

Word count with grep
 
Hi.
I've got a text file with some data in it and I want to know how many times the letter 'x' occurs in it, my problem is that x occurs many times on one line so I can't use grep -c.
What's the best way to do this? I'm sure there must be a simple solution, but I can't seem to find it!
Thanks for your help.

Matir 02-13-2006 12:03 PM

Code:

cat file | tr -d -c "x" | wc -c
This uses tr to delete all non-x characters and wc to count the xs.

DiagonalArg 02-13-2006 12:14 PM

Thanks! I'm quite pleased to see it wasn't a really obvious solution that I'd missed.

Matir 02-13-2006 12:46 PM

Nope, not terribly obvious. But I do like my shell tricks.


All times are GMT -5. The time now is 11:31 AM.