Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does "wc -w < file.txt" work?

Tags:

linux

unix

wc

I was trying to get only the number of words in a file using wc. wc -w file.txt gives me that plus the file name. I don't want the file name. So, I saw that wc -w < file.txt works.

I don't understand how this command works. I cannot even add a comment below the answer where I saw this command.

Why does it not print filename in the case of wc -w < file.txt?

like image 860
Steam Avatar asked Aug 05 '13 21:08

Steam


People also ask

What is wc file txt?

wc stands for word count. As the name implies, it is mainly used for counting purpose. It is used to find out number of lines, word count, byte and characters count in the files specified in the file arguments. By default it displays four-columnar output.

What is wc W?

wc -w : prints the number of words in a file. wc -c : Displays the count of bytes in a file. wc -m : prints the count of characters from a file.

What does wc file do?

wc tells you how large a text document is. It counts the number of newlines, words, characters, and bytes in text files. If you specify multiple files, wc produces counts for each file, plus totals for all files. If you do not specify any files, wc reads from the standard input (stdin).

What does the given command do wc test TXT Newfile?

Explanation: > symbol can be used with wc command for redirecting output. For example, the following command will read the input from sample. txt and redirect its output to newfile.


2 Answers

wc -w will output the word count and file name for each of its arguments. So the command wc -w myfile.txt will give you something like:

42 myfile.txt

However, where wc doesn't know the filename, it simply outputs the count. You can hide the file name from it by using input redirection as wc is one of those commands that will read standard input if you don't explicitly name a file. This can be done with:

wc -w <myfile.txt

or:

cat myfile.txt | wc -w

although the latter is inefficient in this case and better where the input is more complex than just the contents of some file.

The reason that wc doesn't know the file name in wc -w <myfile.txt is because the shell opens the file and attaches it to the standard input of the wc process. The shell also never passes the <myfile.txt along to the wc program's command line arguments. Many programs like wc have code along the lines of:

FILE * infile;
if (argc == 0)
    infile = stdin;
else
    infile = fopen (argv[1]], "r");

to select either standard input or open a known file name. In the former case, no name is available to the program.

like image 174
paxdiablo Avatar answered Sep 28 '22 15:09

paxdiablo


This works because when you use < the shell is sourcing the contents of "file.txt" to the standard input of wc.

Also, note that wc -l will give you the number of lines in file.txt. If you really want the number of words irrespective on which line they are, you should use wc -w instead.

When you used wc -w file.txt it gave you the number of words inside "file.txt" and the filename because wc accepts many files as possible inputs, so it outputs the filename after the word count for you to know that file has those many words. For example, suppose you have these two files with their respective contents:

twoword.txt: foo bar

threeword.txt: foo bar test

then wc -w twoword.txt threeword.txt would give you:

2 twoword.txt
3 threeword.txt
5 total

thus showing how many words are there in each file then summing up them for total.

like image 23
Claudio Avatar answered Sep 28 '22 15:09

Claudio