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 am looking for a command to count number of all words in a file. For instance if a file is like this,

today is a 
good day

then it should print 5, since there are 5 words there.

share|improve this question
5  
Have you tried wc -w $FILE ? –  don_crissti Jun 19 '13 at 17:07
2  
Don't solve what's been solved. –  Prasanth Jun 19 '13 at 17:31

3 Answers 3

up vote 20 down vote accepted

The command wc aka. word count can do it:

$ wc -w <file>

example

$ cat sample.txt
today is a 
good day


$ wc -w sample.txt
5 sample.txt


# just the number (thanks to Stephane Chazelas' comment)
$ wc -w < sample.txt
5
share|improve this answer
    
Note that words for wc -w don't have the same definition as for GNU grep -w. For wc a word is a sequence of one or more non-space characters ([:space:] character class in the current locale). For instance foo,bar and foo bar (with a non-breaking space) are each one word. –  Stéphane Chazelas Jun 12 at 15:18

I came up with this for JUST the number:

wc -w [file] | cut -d' ' -f1

5

I also like the wc -w < [file] approach

share|improve this answer
13  
wc -w < "$file" for JUST the number. –  Stéphane Chazelas Jun 19 '13 at 20:24

The better solution is using Perl:

perl -nle '$word += scalar(split(/\s+/, $_)); END{print $word}' filename

@Bernhard

You can check the source code of wc command from coreutils, I test in my machine, with file subst.c in bash 4.2 source.

time wc -w subst.c

real    0m0.025s
user    0m0.016s
sys     0m0.000s

And

time perl -nle '$word += scalar(split(" ", $_)); END{print $word}' subst.c

real    0m0.021s
user    0m0.016s
sys     0m0.004s

The bigger the file is, the more efficient Perl is with respect to wc.

share|improve this answer
13  
Why is this better than wc? –  Sparr Jun 19 '13 at 17:13
    
wc need open entire file before it processes, it will cause problem when you work with large file. –  Gnouc Jun 19 '13 at 17:20
1  
@Sparr for one thing because, to my very great surprise, it seems to be much faster. I tried it on a text file with 141813504 words and wc took ~14sec while Perl took ~5sec! –  terdon Jun 19 '13 at 17:28
3  
I think the 'bigger' issue really is an answer that has a dependency on Perl and I'm never a big fan of such a dependency. If the question was about performance that would be another thing. –  Michael Durrant Jun 19 '13 at 17:31
5  
Note that a split on /\s+/ is like a split(' ') except that any leading whitespace produces a null first field. That difference will give you one extra word (the null first field, that is) per line link. So use (split(" ", $_)) otherwise for a file created like this: echo -e "unix\n linux" > testfile your one-liner reports 3 words. –  don_crissti Jun 19 '13 at 18:03

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.