5

I have been trying to create a file called ~/dictwords.txt, which contains the number of words found in the file /usr/share/dict/words.

I already created the file then I removed it because I did it wrong I originally did the echo command. Then I tried this command and was so excited because I thought I was correct.

wc -w /usr/share/dict/words > ~/dictwords.txt

It's still wrong, I am supposed to have just the number, without a file name.

  • 2
    Solution: use wc -w < /usr/share/dict/words > ~/dictwords.txt. Done. – gniourf_gniourf Jun 25 '14 at 18:17
4

By default, wc print result along with filenames. If you want only the result, you must make wc read input from stdin:

</usr/share/dict/words wc -w > ~/dicwords.txt

With your current solution, you can use some other tools to get only the result from wc, like cut, awk, grep...

wc -c /usr/share/dict/words | cut -d' ' -f1 > ~/dicwords.txt
1

Here is a solution using cat, which avoids printing the filename after the result.

cat /usr/share/dict/words | wc -w
  • @StephenKitt: What additional explanation do you want? It seems pretty thorough and useful to me. – Flimzy Dec 7 '18 at 12:08
  • @Flimzy it’s effectively the same as </usr/share/dict/words wc -w in the accepted answer, so as-is it’s not useful. – Stephen Kitt Dec 7 '18 at 12:33
  • No, it's quite different. As explained (in not so many words), by reading stdin, wc omits the filename in the output. – Flimzy Dec 7 '18 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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