我想使用wc -l但只输出数字来计算文件中的行数,没有以下空格和文件名。从评论中编辑:文件名中可能包含数字。
目前,如果我这样做:
wc -l /path/to/file
Run Code Online (Sandbox Code Playgroud)
在一个有 176 行的文件上,输出是:
176 /path/to/file
Run Code Online (Sandbox Code Playgroud)
这是在 bash 脚本中,结果数字将被分配为变量的值。
kos*_*kos 12
我会用:
<file wc -l
Run Code Online (Sandbox Code Playgroud)
相反,cat file | wc -l不需要分叉外壳并运行另一个进程(并且运行速度更快):
<file wc -l
Run Code Online (Sandbox Code Playgroud)
(/tmp/ramdisk/file存储在 ramdisk 中以消除 I/O 和缓存。)
然而,对于小文件来说,差异确实是可以忽略的。
另一种方式是:
wc -l file | cut -d ' ' -f 1
Run Code Online (Sandbox Code Playgroud)
在我的测试中,它的性能与<file wc -l.
您可以使用 cat 使用简单的单衬纸完成此操作。
cat deluge1.log | wc -l
答案太简单,不需要冗长的答案。
| 归档时间: |
|
| 查看次数: |
9940 次 |
| 最近记录: |