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

2

9

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?

Commercial answered 3/10, 2016 at 11:27 Comment(0)
M
10

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.

Marceau answered 3/10, 2016 at 11:36 Comment(0)
T
0

better use awk '{ print }' demo| wc -l

Toledo answered 5/2, 2020 at 9:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.