我从wc -l
命令得到了错误的结果。经过长时间的检查,我发现了问题的核心,这里是模拟:
$ echo "line with end" > file
$ echo -n "line without end" >>file
$ wc -l file
1 file
这里有两行文字,但是最后一个 "\n" 缺失了。是否有简单的解决方法?
$ grep -c . file #count the occurrence of any character
2
以上不会计算空行。如需计算空行,请使用
$ grep -c '^' file #count the beginnings of the lines
2
wc
的手册页面:
-l, --lines
print the newline counts
echo
的手册页面中的表单
-n do not output the trailing newline
你的文件中有 1
个换行符,所以 wc -l
显示了 1
。
你可以使用以下的 awk
命令来计算行数。
awk 'END{print NR}' file
echo -n
,为什么不能在echo -n
的结尾添加\n
? - Billwc
命令的文档却不这样认为。 - paxdiablo