word count issue
Hello everyone,
What is the command to find the number of word *FOO* in a given file (e.g. goo.txt)? thanks in advance, George |
You have to combine some commands. If you can split the file into one word per line, then you can pipe that into grep -c.
The tr command can be used to do that splitting. You could also use awk, perl or sed or some other tools, but tr is probably the smallest program to invoke, so it's a good choice. In this example we split on spaces or tabs. It is simple to add other word splitting characters if you wish. Code:
tr ' \t' '\n\n' input_file |grep -c '^foo$' |
Thanks matthewg42,
Quote:
tr ' \t' '\n\n' regards, George |
Assuming GNU grep and no embedded newlines in FOO:
Code:
grep -o '\bFOO\b' goo.txt|wc -l (that's why wc -l). |
Thanks radoulov,
Why you add \b before and after FOO? Quote:
regards, George |
Quote:
consider this: Code:
$ print 'FOO,FOO Edit: GNU grep has the -w option with the same meaning. |
Quote:
Consider these examples: Code:
% echo "This is my input string" | tr 'itp' 'IT_' Code:
ThIs Is my In_uT sTrIng Code:
% echo "This is my input string" | tr '[a-j]' '[0-9]' Code:
% echo "This is my input string" | tr ' \t' '\n\n' Code:
tr ' \t' '\n\n' < input_file |grep -cx 'foo' |
All times are GMT -5. The time now is 04:45 AM. |