Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a large JSON file that is on one line, and I want to use the command line to be able to count the number of occurrences of a word in the file. How can I do that?

share|improve this question

3 Answers 3

up vote 9 down vote accepted
$ tr ' ' '\n' < FILE | grep WORD | wc -l

Where tr replaces spaces with newlines, grep filters all resulting lines matching WORD and wc counts the remaining ones.

One can even save the wc part using the -c option of grep:

$ tr ' ' '\n' < FILE | grep -c WORD

The -c option is defined by POSIX.

If it is not guaranteed that there are spaces between the words, you have to use some other character (as delimiter) to replace. For example alternative tr parts are

tr '"' '\n'

or

tr "'" '\n'

if you want to replace double or single quotes.

In case you need to count WORD but not prefixWORD, WORDsuffix or prefixWORDsuffix, you can enclose the WORD pattern in word-begin/end markers:

grep -c '\<WORD\>'
share|improve this answer
    
what if there are no spaces, i.e. the field name is surrounded by quotes? e.g. "field" –  mythz Sep 19 '10 at 16:42
    
@mythz: Then you replace the quotes with newlines with tr. I'll update the answer. –  maxschlepzig Sep 19 '10 at 16:45
    
yep did the trick :) –  mythz Sep 19 '10 at 16:52
    
This answer is incorrect in many ways. It is vague: you should explain how to come up with a tr command that does the job instead of suggesting examples that will never work in all situations. It will also match words that contain the word you are looking for. The grep -o '\<WORD\>' | wc -l solution is far superior. –  Sam Hocevar Apr 9 '11 at 2:28
1  
@Sam, the question leaves it kind of open, if a searched word should be searched like 'WORD' or '\<WORD\>' - you can read it both ways. Even if you read it the 2nd way and only in the 2nd way, then my answer would be only incorrect in 1 one way. ;) And the 'grep -o' solution is only superior, if it supports the -o option - which is not specified by POSIX ... Well, I don't think so that the use of tr is that exotic to call it vague ... –  maxschlepzig May 6 '11 at 21:01

With GNU grep, grep -o '\<WORD\>' | wc -l works. (\< asserts the start of a word and \> asserts the end of a word (similar to Perl's \b), so this ensures that you're not matching a string in the middle of a word.)

For example,

$ python -c 'import this' | grep '\<one\>'
There should be one-- and preferably only one --obvious way to do it.
Namespaces are one honking great idea -- let's do more of those!
$ python -c 'import this' | grep -o '\<one\>'
one
one
one
$ python -c 'import this' | grep -o '\<one\>' | wc -l
3
share|improve this answer

With recent versions of GNU coreutils,

grep -o -c WORD file

should work. (Earlier versions had a bug in this combination of options.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.