Alu*_*ard 994 linux bash scripting command-line
我有这样的线条,我想知道我实际上有多少行......
09:16:39 AM all 2.00 0.00 4.00 0.00 0.00 0.00 0.00 0.00 94.00
09:16:40 AM all 5.00 0.00 0.00 4.00 0.00 0.00 0.00 0.00 91.00
09:16:41 AM all 0.00 0.00 4.00 0.00 0.00 0.00 0.00 0.00 96.00
09:16:42 AM all 3.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 96.00
09:16:43 AM all 0.00 0.00 1.00 0.00 1.00 0.00 0.00 0.00 98.00
09:16:44 AM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
09:16:45 AM all 2.00 0.00 6.00 0.00 0.00 0.00 0.00 0.00 92.00
Run Code Online (Sandbox Code Playgroud)
有没有办法用linux命令统计它们?
use*_*509 1881
用途wc:
wc -l <filename>
Run Code Online (Sandbox Code Playgroud)
这将输出以下行数<filename>:
$ wc -l /dir/file.txt
3272485 /dir/file.txt
Run Code Online (Sandbox Code Playgroud)
或者,<filename>从结果使用中省略wc -l < <filename>:
$ wc -l < /dir/file.txt
3272485
Run Code Online (Sandbox Code Playgroud)
您也可以管道数据wc:
$ cat /dir/file.txt | wc -l
3272485
$ curl yahoo.com --silent | wc -l
63
Run Code Online (Sandbox Code Playgroud)
Lau*_*ira 136
要统计所有线路使用:
$ wc -l file
Run Code Online (Sandbox Code Playgroud)
仅过滤和计算具有模式使用的行:
$ grep -w "pattern" -c file
Run Code Online (Sandbox Code Playgroud)
或者使用-v来反转匹配:
$ grep -w "pattern" -c -v file
Run Code Online (Sandbox Code Playgroud)
请参阅grep手册页以查看-e,-i和-x args ...
Joh*_*ica 71
wc -l <file.txt>
Run Code Online (Sandbox Code Playgroud)
要么
command | wc -l
Run Code Online (Sandbox Code Playgroud)
gho*_*g74 42
有很多方法.使用wc是一个.
wc -l file
其他包括
awk 'END{print NR}' file
sed -n '$=' file (GNU sed)
grep -c ".*" file
Run Code Online (Sandbox Code Playgroud)
the*_*guy 25
该工具wc是UNIX和类UNIX操作系统中的"字计数器",您还可以使用它来计算文件中的行数,通过添加-l选项,因此wc -l foo将计算行数foo.您还可以通过以下程序管道输出:ls -l | wc -l,它将告诉您当前目录中有多少文件.
sto*_*ren 23
如果要检查目录中所有文件的总行,可以使用find和wc:
find . -type f -exec wc -l {} +
Run Code Online (Sandbox Code Playgroud)
ggb*_*667 15
如果你想要的只是行数(而不是行数和返回的愚蠢文件名):
wc -l < /filepath/filename.ext
Run Code Online (Sandbox Code Playgroud)
如前所述,这些也有效(但由于其他原因而劣质):
awk 'END{print NR}' file # not on all unixes
sed -n '$=' file # (GNU sed) also not on all unixes
grep -c ".*" file # overkill and probably also slower
Run Code Online (Sandbox Code Playgroud)
Chi*_*iru 15
wc -l 不计算行。是的,这个答案可能有点晚了,但我还没有发现任何人在答案中记录了更强大的解决方案。
与流行的看法相反,POSIX 根本不需要文件以换行符结尾。是的,POSIX 3.206 行的定义如下:
零个或多个非 <newline> 字符加上终止字符的序列。
然而,很多人不知道的是,POSIX 还将POSIX 3.195 Incomplete Line定义为:
文件末尾的一个或多个非 <newline> 字符的序列。
因此,没有尾随的文件LF完全符合 POSIX 标准。
如果您选择不支持这两种 EOF 类型,则您的程序不符合 POSIX 标准。
作为一个例子,让我们看看下面的文件。
1 This is the first line.
2 This is the second line.
Run Code Online (Sandbox Code Playgroud)
不管 EOF,我相信你会同意有两行。您是通过查看已启动的行数而不是查看已终止的行数来计算的。换句话说,根据 POSIX,这两个文件都具有相同的行数:
1 This is the first line.\n
2 This is the second line.\n
Run Code Online (Sandbox Code Playgroud)
1 This is the first line.\n
2 This is the second line.
Run Code Online (Sandbox Code Playgroud)
手册页关于wc计算换行符的内容相对清晰,换行符只是一个0x0a字符:
NAME
wc - print newline, word, and byte counts for each file
Run Code Online (Sandbox Code Playgroud)
因此,wc甚至不尝试计算您可能称之为“线”的东西。使用wc来计算行数很可能会导致错误计数,具体取决于输入文件的 EOF。
您可以使用上面的示例grep来计算行数。这个解决方案更健壮和精确,它支持文件中一行的所有不同风格:
$ grep -c ^ FILE
Run Code Online (Sandbox Code Playgroud)
小智 8
使用nl这样:
nl filename
Run Code Online (Sandbox Code Playgroud)
来自man nl:
将每个FILE写入标准输出,并添加行号.如果没有FILE,或者FILE是 - ,则读取标准输入.
我一直在用这个:
cat myfile.txt | wc -l
Run Code Online (Sandbox Code Playgroud)
我更喜欢它而不是接受的答案,因为它不打印文件名,你不必用它awk来修复它.接受的答案:
wc -l myfile.txt
但我认为最好的是GGB667的答案:
wc -l < myfile.txt
我可能会从现在开始使用它.它比我的方式略短.如果有人喜欢的话,我会用我原来的方式做这件事.这两种方法的输出相同.
小智 5
以上是首选方法,但"cat"命令也可以提供帮助:
cat -n <filename>
Run Code Online (Sandbox Code Playgroud)
将显示包含行号的文件的全部内容.
我在寻找一种计算多个文件行的方法时看到了这个问题,所以如果要计算.txt文件的多个文件行,你可以这样做,
cat *.txt | wc -l
Run Code Online (Sandbox Code Playgroud)
它也将在一个.txt文件上运行;)
wc -l <filename>
这将为您提供输出中的行数和文件名。
例如。
wc -l 24-11-2019-04-33-01-url_creator.log
输出
63 24-11-2019-04-33-01-url_creator.log
使用
wc -l <filename>|cut -d\ -f 1
仅获取输出中的行数。
例如。
wc -l 24-11-2019-04-33-01-url_creator.log|cut -d\ -f 1
输出
63