简体   繁体   English

为什么“wc”命令说我在一个文件中只有一行,而实际上实际上有两行?

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

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).如您所见,我有 3 个字符( man : 6d 61 6e),后跟一个换行符( \\n : 0a),然后是另外三个( car : 63 61 75),以 NUL 字符(00)结尾。 Clearly, there are two lines in that file, but the wc command reports that the file has got only one.显然,该文件中有两行,但 wc 命令报告该文件只有一行。 What gives?什么给? Or do you think that in order to qualify as a line in Unix you must be terminated with a newline character?或者你认为为了在 Unix 中成为一行,你必须以换行符结尾? NUL doesn't count? NUL 不算?

Or do you think that in order to qualify as a line in Unix you must be terminated with a newline character?或者你认为为了在 Unix 中成为一行,你必须以换行符结尾?

Actually, yes - even POSIX says that:实际上,是的 - 甚至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. wc 实用程序应读取一个或多个输入文件,并默认将每个输入文件中包含的换行符、字和字节数写入标准输出。

better use awk '{ print }' demo| wc -l最好使用awk '{ print }' demo| wc -l awk '{ print }' demo| wc -l

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM