Why is the "wc" command saying that I've got only one line in a file while in fact there are really two?

user4087080 picture user4087080 · Oct 3, 2016 · Viewed 8k times · Source

Take a look at this example please:

$ cat < demo
man
car$ 
$ 
$ od -x < demo 
0000000 616d 0a6e 6163 0072
0000007
$ 
$ wc < demo 
1 2 7

As you can see, I've got there 3 characters (man: 6d 61 6e) followed by a newline (\n: 0a) and then another three (car: 63 61 75) terminated with a NUL character (00). Clearly, there are two lines in that file, but the wc command reports that the file has got only one. What gives? Or do you think that in order to qualify as a line in Unix you must be terminated with a newline character? NUL doesn't count?

Answer

Arkadiusz Drabczyk picture Arkadiusz Drabczyk · Oct 3, 2016

Or do you think that in order to qualify as a line in Unix you must be terminated with a newline character?

Actually, yes - even POSIX says that:

The wc utility shall read one or more input files and, by default, write the number of newlines, words, and bytes contained in each input file to the standard output.