为什么 "wc" 命令说文件中只有一行,而实际上有两行?
Why is the "wc" command saying that I've got only one line in a file while in fact there are really two?
请看这个例子:
$ cat < demo
man
car$
$
$ od -x < demo
0000000 616d 0a6e 6163 0072
0000007
$
$ wc < demo
1 2 7
如您所见,我得到了 3 个字符 (man
: 6d 61 6e),后跟一个换行符 (\n
: 0a),然后是另外三个字符 (car
: 63 61 75) 以 NUL 字符 (00) 结尾。显然,该文件中有两行,但 wc 命令报告该文件只有一行。是什么赋予了?或者你认为为了在 Unix 中有资格成为一行,你必须以换行符结束? NUL 不算数?
Or do you think that in order to qualify as a line in Unix you must be
terminated with a newline character?
实际上,是的 - 甚至 POSIX
也这么说:
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.
更好用
awk '{ print }' demo| wc -l
请看这个例子:
$ cat < demo
man
car$
$
$ od -x < demo
0000000 616d 0a6e 6163 0072
0000007
$
$ wc < demo
1 2 7
如您所见,我得到了 3 个字符 (man
: 6d 61 6e),后跟一个换行符 (\n
: 0a),然后是另外三个字符 (car
: 63 61 75) 以 NUL 字符 (00) 结尾。显然,该文件中有两行,但 wc 命令报告该文件只有一行。是什么赋予了?或者你认为为了在 Unix 中有资格成为一行,你必须以换行符结束? NUL 不算数?
Or do you think that in order to qualify as a line in Unix you must be terminated with a newline character?
实际上,是的 - 甚至 POSIX
也这么说:
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.
更好用
awk '{ print }' demo| wc -l