简体   繁体   English

mac 的 WC 命令显示少一个结果

[英]WC command of mac showing one less result

I have a text file which has over 60MB size.我有一个超过 60MB 的文本文件。 It has got entries in 5105043 lines, but when I am doing wc -l it is giving only 5105042 results which is one less than actual.它有 5105043 行的条目,但是当我执行 wc -l 时,它只给出 5105042 个结果,比实际结果少一个。 Does anyone have any idea why it is happening?有谁知道为什么会这样?

Is it a common thing when the file size is large?当文件很大时,这是常见的事情吗?

Last line does not contain a new line.最后一行不包含新行。

One trick to get the result you want would be:获得您想要的结果的一个技巧是:

sed -n '=' <yourfile> | wc -l

This tells sed just to print the line number of each line in your file which wc then counts.这告诉sed只打印文件中每一行的行号,然后wc计数。 There are probably better solutions, but this works.可能有更好的解决方案,但这是有效的。

The last line in your file is probably missing a newline ending.文件中的最后一行可能缺少换行符结尾。 IIRC, wc -l merely counts the number of newline characters in the file. IIRC, wc -l仅计算文件中换行符的数量。

If you try: cat -A file.txt | tail如果您尝试: cat -A file.txt | tail cat -A file.txt | tail does your last line contain a trailing dollar sign ( $ )? cat -A file.txt | tail您的最后一行是否包含尾随美元符号 ( $ )?

EDIT:编辑:

Assuming the last line in your file is lacking a newline character, you can append a newline character to correct it like this:假设文件中的最后一行缺少换行符,您可以附加一个换行符来更正它,如下所示:

printf "\n" >> file.txt

The results of wc -l should now be consistent. wc -l的结果现在应该是一致的。

60 MB seems a bit big file but for small size files. 60 MB 似乎有点大文件,但对于小文件。 One option could be一种选择可能是

cat -n file.txt

OR要么

cat -n sample.txt | cut -f1 | tail -1

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

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