bash 文件中最长的一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1655372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Longest line in a file
提问by Andrew Prock
I'm looking for a simple way to find the length of the longest line in a file. Ideally, it would be a simple bash shell command instead of a script.
我正在寻找一种简单的方法来查找文件中最长行的长度。理想情况下,它将是一个简单的 bash shell 命令而不是脚本。
回答by Daniel
Using wc (GNU coreutils) 7.4:
使用 wc (GNU coreutils) 7.4:
wc -L filename
gives:
给出:
101 filename
回答by Pale Blue Dot
awk '{print length, awk '{ if (length(#!/bin/sh
MAX=0 IFS=
while read -r line; do
if [ ${#line} -gt $MAX ]; then MAX=${#line}; fi
done < ""
printf "$MAX\n"
) > max) {max = length(wc -L < filename
); maxline = 101
} } END { print maxline }' YOURFILE
}' Input_file |sort -nr|head -1
For reference : Finding the longest line in a file
供参考:查找文件中最长的行
回答by Ramon
perl -ne 'print length()." line $. $_"' myfile | sort -nr | head -n 1
回答by Jens
Just for fun and educational purpose, the pure POSIX shell solution, without useless use of cat and no forking to external commands. Takes filename as first argument:
只是为了娱乐和教育目的,纯 POSIX shell 解决方案,没有无用的使用 cat 并且没有分叉到外部命令。将文件名作为第一个参数:
perl -ne 'print length()." line $. $_"' myfile | sort -n
回答by Anonymous
wc -L <"${SourceFile}"
# or
expand --tabs=8 "${SourceFile}" | awk '{ if (length( expand --tabs=1 "${SourceFile}" | wc -L
# or
awk '{ if (length( Expanded nonexpanded
$'nn\tnn' 10 5
) > max) {max = length($ cat -n test.txt | awk '{print "longest_line_number: " " length_with_line_number: " length}' | sort -k4 -nr | head -3
longest_line_number: 3 length_with_line_number: 13
longest_line_number: 4 length_with_line_number: 12
longest_line_number: 2 length_with_line_number: 11
)} } END { print max }' "${SourceFile}"
) > max) {max = length(cat filename | awk '{print length, perl -ne 'print ($l = $_) if (length > length($l));' filename | tail -1
}'|sort -nr|head -1
)} } END { print max }'
gives
给
##代码##回答by Chris Koknat
Prints the length, line number, and contents of the longest line
打印最长行的长度、行号和内容
##代码##Prints a sorted list of all lines, with line numbers and lengths
打印所有行的排序列表,包含行号和长度
.
is the concatenation operator - it is used here after length()$.
is the current line number$_
is the current line
.
是连接运算符 - 在这里使用 length() 之后$.
是当前行号$_
是当前行
回答by John Kearney
Important overlooked point in the above examples.
上面例子中被忽视的重要一点。
The following 2 examples count expanded tabs
以下 2 个示例计算扩展选项卡
##代码##The following 2 count non expaned tabs.
以下 2 个计数非扩展选项卡。
##代码##so
所以
##代码##回答by wangf
Looks all the answer do not give the line number of the longest line. Following command can give the line number and roughly length:
看起来所有的答案都没有给出最长行的行号。以下命令可以给出行号和大致长度:
##代码##回答by Nadir SOUALEM
Here are references of the anwser
这是anwser的参考
##代码##回答by rsp
In perl:
在 perl 中:
##代码##this only prints the line, not its length too.
这只会打印行,而不是它的长度。