不是空文件,但是“wc -l”命令输出为0。

3

我有一个非空文件(即使是大文件,400Ko),可以使用less命令阅读。

但如果我尝试使用wc -l /path/to/file命令输出行数,则会输出0

这可能怎么可能呢?


2
我猜你的文件是在Windows/Dos中生成的,它包含Dos换行符,这些换行符wc无法检测到。首先要进行转换。 - Kent
2
wc 计算换行符的数量 - 可能是一行。 - Mark Setchell
1
没有 -l 选项的 wc 的输出是什么?wc -L 的输出是什么? - Leon
1
@Simon请报告 file yourFile - Kent
1
这里的任何答案都只是猜测,如果没有文件维护的详细信息。 - Charles Duffy
显示剩余9条评论
4个回答

4
您可以自行验证该文件不包含换行符(ASCII 10),否则将导致wc -l报告0行。
  1. First, count the characters in your file:

    wc -c /path/to/file
    

    You should get a non-zero value.

  2. Now, filter out everything that isn't a newline:

    tr -dc '\n' /path/to/file | wc -c
    

    You should get back 0.

  3. Or, delete the newlines and count the result.

    tr -d '\n' | wc -c
    

    You should get back the same value as in step 1.


3

2
这是一种可能的方法。制作一个只包含 null 的 400k 文件:
dd if=/dev/zero bs=1024 count=400 of=/tmp/nulls ; ls -log /tmp/nulls 

输出结果显示该文件已存在:

400+0 records in
400+0 records out
409600 bytes (410 kB, 400 KiB) copied, 0.00343425 s, 119 MB/s
-rw-rw-r-- 1 409600 Feb 28 11:12 /tmp/nulls

现在数一数行数:
wc -l /tmp/nulls
0 /tmp/nulls

@Kent,关于“less”:hexdump -C /tmp/nulls | less。 关于“html”:sensible-browser /tmp/nulls显然表明了它的内容。 当然,你是正确的,它可能是Dos换行符,但这个答案展示了最简单的400K实例。 - agc

2
如果HTML文件是minified,那么这是可能的。在内容最小化期间,换行符将被删除。
可以试试用file命令。
file filename.html

filename.html: HTML document text, UTF-8 Unicode text, with very long lines, with no line terminators

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接