在使用wc -L
的Linux命令中,可以获得文本文件最长行的长度。
如何找到文本文件最短行的长度?
发布于 2012-09-26 03:09:57
试试这个:
awk '{print length}' <your_file> | sort -n | head -n1
此命令获取所有文件的长度,对它们进行排序(正确地,作为数字),并在控制台上打印最小的数字。
发布于 2012-09-26 03:17:40
纯awk解决方案:
awk '(NR==1||length<shortest){shortest=length} END {print shortest}' file
发布于 2015-12-14 16:41:01
上述两种awk
解决方案都不像wc -L
那样处理“\r”。对于单行输入文件,它们不应该产生大于wc -L
报告的最大行长的值。
这是一种新的基于sed
的解决方案(我无法在保持正确的同时缩短):
echo $((`sed 'y/\r/\n/' file|sed 's/./#/g'|sort|head -1|wc --bytes`-1))
下面是一些示例,展示了'\r‘声明并演示了sed
解决方案:
$ echo -ne "\rABC\r\n" > file
$ wc -L file
3 file
$ awk '{print length}' file|sort -n|head -n1
5
$ awk '(NR==1||length<shortest){shortest=length} END {print shortest}' file
5
$ echo $((`sed 'y/\r/\n/' file|sed 's/./#/g'|sort|head -1|wc --bytes`-1))
0
$
$ echo -ne "\r\r\n" > file
$ wc -L file
0 file
$ echo $((`sed 'y/\r/\n/' file|sed 's/./#/g'|sort|head -1|wc --bytes`-1))
0
$
$ echo -ne "ABC\nD\nEF\n" > file
$ echo $((`sed 'y/\r/\n/' file|sed 's/./#/g'|sort|head -1|wc --bytes`-1))
1
$
https://stackoverflow.com/questions/12600177
复制