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.

0
7

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 -w /usr/share/dict/words | cut -d' ' -f1 > ~/dicwords.txt

Though that assumes an implementation of wc that doesn't add space characters before the number, which is not the case of every wc implementation.

2

The file /usr/share/dict/words ought to contain one word per line. On some systems I imagine that it could possibly contain one "dictionary term" per line and that each of these may be made up from more than one space-delimited word, but I have never seen this myself.

You get the number of lines with one of

  • wc -l </usr/share/dict/words (counts the number of newlines in the file)
  • sed -n '$=' /usr/share/dict/words (at the last line, output the current line number)
  • awk 'END { print NR }' /usr/share/dict/words (at the end of input, output the number of records (lines) read)
  • grep -c '[^[:space:]]' /usr/share/dict/words (count the number of lines that have at least one non-space(-like) character)

Of these, the wc -l variation is least portable as it may output, or not output, extra spaces before and/or after the actual number.

0
0

If you will need to use the words count in the script, you can save the value into a variable:

numWords=$(wc -w /usr/share/dict/words | tr -dc '0-9')

else, you can save it into the file:

wc -w /usr/share/dict/words | tr -dc '0-9' > ~/dicwords.txt
1
  • 1
    This would potentially produce unexpected results if the pathname of the input contained digits. In the case of /usr/share/dict/words it's obviously fine.
    – Kusalananda
    Sep 2 at 16:10
0
awk 'BEGIN{sum=0}{sum=sum+NF}END{print sum}' /usr/share/dict/words >~/dictwords.txt
-1

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

cat /usr/share/dict/words | wc -w
5
  • @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. 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
  • It also reads the file into a pipe and feeds it back out out into wc, which is minor but still worse than using cut and friends on wc's result. You can save a few keystrokes, I guess.
    – Backgammon
    Sep 25 '19 at 15:52
  • 1
    @Flimzy Using a redirection into wc, as Stephen showed in his comment, makes wc read the file from standard input.
    – Kusalananda
    Sep 2 at 16:10

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.