文件中最长的行
我正在寻找一种简单的方法来查找文件中最长行的长度。理想情况下,它是一个简单的 bash shell 命令而不是脚本。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
看起来所有答案都没有给出最长行的行号。以下命令可以给出行号和大致长度:
Looks all the answer do not give the line number of the longest line. Following command can give the line number and roughly length:
上面例子中被忽视的重要一点。
以下 2 个示例对展开的选项卡进行计数
以下 2 个示例对非展开的选项卡进行计数。
所以
Important overlooked point in the above examples.
The following 2 examples count expanded tabs
The following 2 count non expaned tabs.
so
在 perl 中:
这只打印行,而不打印它的长度。
In perl:
this only prints the line, not its length too.
以下是 anwser 的参考
http://wtanaka.com/node/7719
Here are references of the anwser
http://wtanaka.com/node/7719
主题的变化。
这将显示文件中找到的最长行长度的所有行,并保留它们在源代码中出现的顺序。
所以 myfile
会给
Variation on the theme.
This one will show all lines having the length of the longest line found in the file, retaining the order they appear in the source.
So myfile
will give
只是为了好玩,这里是 Powershell 版本:
为了获取长度:
Just for fun, here's the Powershell version:
And to just get the length:
我在 Unix 环境中,使用大小为几 GB 的 gzip 压缩文件。我使用记录长度为 2052 的 2 GB gzipped 文件测试了以下命令
和
zcat| awk '{打印长度}' | sort -u
平均时间
117 秒
109 秒
这是我运行大约 10 次后的脚本。
I'm in a Unix environment, and work with gzipped files that are a few GBs in size. I tested the following commands using a 2 GB gzipped file with record length of 2052.
zcat <gzipped file> | wc -L
and
zcat <gzipped file> | awk '{print length}' | sort -u
The times were on avarage
117 seconds
109 seconds
Here is my script after about 10 runs.
如果您使用的是 MacOS 并收到此错误:
wc:非法选项 -- L
您不需要安装 GNU sipmly 即可执行此操作。如果您只想获取文件最长行中的字符数并且您使用的是 OS X,请运行:
awk '{print length}' "$file_name" |排序-rn | head -1
像这样的东西;
echo "文件 $file_name 中最长的行有 $(awk '{print length}' "$file_name" | sort -rn | head -1) 个字符"
输出:
最长的行文件 my_file 有 117 个字符
If you are using MacOS and are getting this error:
wc: illegal option -- L
you dont need to install GNU sipmly do this.If all you want to do is just get the count of the characters in the longest line of the file and you are using OS X run:
awk '{print length}' "$file_name" | sort -rn | head -1
Something like this;
echo "The longest line in the file $file_name has $(awk '{print length}' "$file_name" | sort -rn | head -1) characters"
Outputs:
The longest line in the file my_file has 117 characters
使用 wc (GNU coreutils) 7.4:
给出:
Using wc (GNU coreutils) 7.4:
gives:
供参考:查找文件中最长的行
For reference : Finding the longest line in a file
只是出于娱乐和教育目的,纯 POSIX shell 解决方案,没有无用的 cat 使用,也没有分叉到外部命令。将文件名作为第一个参数:
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:
给出
gives
打印最长行的长度、行号和内容
打印所有行的排序列表,其中包含行号和长度
。
是连接运算符 - 它在 length() 之后使用$.
是当前行号$_
是当前行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