文件中最长的行

发布于 2024-08-09 18:45:38 字数 65 浏览 6 评论 0原文

我正在寻找一种简单的方法来查找文件中最长行的长度。理想情况下,它是一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

心如荒岛 2024-08-16 18:45:39

看起来所有答案都没有给出最长行的行号。以下命令可以给出行号和大致长度:

$ cat -n test.txt | awk '{print "longest_line_number: " $1 " 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

Looks all the answer do not give the line number of the longest line. Following command can give the line number and roughly length:

$ cat -n test.txt | awk '{print "longest_line_number: " $1 " 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
ぇ气 2024-08-16 18:45:39

上面例子中被忽视的重要一点。

以下 2 个示例对展开的选项卡进行计数

  wc -L  <"${SourceFile}" 
# or
  expand --tabs=8 "${SourceFile}" | awk '{ if (length($0) > max) {max = length($0)} } END { print max }'

以下 2 个示例对非展开的选项卡进行计数。

  expand --tabs=1 "${SourceFile}" | wc -L 
# or
  awk '{ if (length($0) > max) {max = length($0)} } END { print max }' "${SourceFile}"

所以

              Expanded    nonexpanded

nn\tnn'       10            5

Important overlooked point in the above examples.

The following 2 examples count expanded tabs

  wc -L  <"${SourceFile}" 
# or
  expand --tabs=8 "${SourceFile}" | awk '{ if (length($0) > max) {max = length($0)} } END { print max }'

The following 2 count non expaned tabs.

  expand --tabs=1 "${SourceFile}" | wc -L 
# or
  awk '{ if (length($0) > max) {max = length($0)} } END { print max }' "${SourceFile}"

so

              Expanded    nonexpanded

nn\tnn'       10            5
灰色世界里的红玫瑰 2024-08-16 18:45:39

在 perl 中:

perl -ne 'print ($l = $_) if (length > length($l));' filename | tail -1

这只打印行,而不打印它的长度。

In perl:

perl -ne 'print ($l = $_) if (length > length($l));' filename | tail -1

this only prints the line, not its length too.

夜唯美灬不弃 2024-08-16 18:45:39

以下是 anwser 的参考

cat filename | awk '{print length, $0}'|sort -nr|head -1

http://wtanaka.com/node/7719

Here are references of the anwser

cat filename | awk '{print length, $0}'|sort -nr|head -1

http://wtanaka.com/node/7719

鹤舞 2024-08-16 18:45:39

主题的变化。

这将显示文件中找到的最长行长度的所有行,并保留它们在源代码中出现的顺序。

FILE=myfile grep `tr -c "\n" "." < $FILE | sort | tail -1` $FILE

所以 myfile

x
mn
xyz
123
abc

会给

xyz
123
abc

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.

FILE=myfile grep `tr -c "\n" "." < $FILE | sort | tail -1` $FILE

So myfile

x
mn
xyz
123
abc

will give

xyz
123
abc
抱猫软卧 2024-08-16 18:45:39

只是为了好玩,这里是 Powershell 版本:

cat filename.txt | sort length | select -last 1

为了获取长度:

(cat filename.txt | sort length | select -last 1).Length

Just for fun, here's the Powershell version:

cat filename.txt | sort length | select -last 1

And to just get the length:

(cat filename.txt | sort length | select -last 1).Length
习惯成性 2024-08-16 18:45:39

我在 Unix 环境中,使用大小为几 GB 的 gzip 压缩文件。我使用记录长度为 2052 的 2 GB gzipped 文件测试了以下命令

  1. 。 | wc -L

  1. zcat| awk '{打印长度}' | sort -u

平均时间

  1. 117 秒

  2. 109 秒

这是我运行大约 10 次后的脚本。

START=$(date +%s) ## time of start

zcat $1 |  wc -L

END=$(date +%s) ## time of end
DIFF=$(( $END - $START ))
echo "It took $DIFF seconds"

START=$(date +%s) ## time of start

zcat $1 |  awk '{print length}' | sort -u

END=$(date +%s) ## time of end
DIFF=$(( $END - $START ))
echo "It took $DIFF seconds"

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.

  1. zcat <gzipped file> | wc -L

and

  1. zcat <gzipped file> | awk '{print length}' | sort -u

The times were on avarage

  1. 117 seconds

  2. 109 seconds

Here is my script after about 10 runs.

START=$(date +%s) ## time of start

zcat $1 |  wc -L

END=$(date +%s) ## time of end
DIFF=$(( $END - $START ))
echo "It took $DIFF seconds"

START=$(date +%s) ## time of start

zcat $1 |  awk '{print length}' | sort -u

END=$(date +%s) ## time of end
DIFF=$(( $END - $START ))
echo "It took $DIFF seconds"
瀞厅☆埖开 2024-08-16 18:45:39

如果您使用的是 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

书信已泛黄 2024-08-16 18:45:38

使用 wc (GNU coreutils) 7.4:

wc -L filename

给出:

101 filename

Using wc (GNU coreutils) 7.4:

wc -L filename

gives:

101 filename
聽兲甴掵 2024-08-16 18:45:38
awk '{print length, $0}' Input_file |sort -nr|head -1

供参考:查找文件中最长的行

awk '{print length, $0}' Input_file |sort -nr|head -1

For reference : Finding the longest line in a file

孤星 2024-08-16 18:45:38
awk '{ if (length($0) > max) {max = length($0); maxline = $0} } END { print maxline }'  YOURFILE 
awk '{ if (length($0) > max) {max = length($0); maxline = $0} } END { print maxline }'  YOURFILE 
寄意 2024-08-16 18:45:38

只是出于娱乐和教育目的,纯 POSIX shell 解决方案,没有无用的 cat 使用,也没有分叉到外部命令。将文件名作为第一个参数:

#!/bin/sh

MAX=0 IFS=
while read -r line; do
  if [ ${#line} -gt $MAX ]; then MAX=${#line}; fi
done < "$1"
printf "$MAX\n"

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:

#!/bin/sh

MAX=0 IFS=
while read -r line; do
  if [ ${#line} -gt $MAX ]; then MAX=${#line}; fi
done < "$1"
printf "$MAX\n"
美人迟暮 2024-08-16 18:45:38
wc -L < filename

给出

101
wc -L < filename

gives

101
〆凄凉。 2024-08-16 18:45:38
perl -ne 'print length()."  line $.  $_"' myfile | sort -nr | head -n 1

打印最长行的长度、行号和内容

perl -ne 'print length()."  line $.  $_"' myfile | sort -n

打印所有行的排序列表,其中包含行号和长度

是连接运算符 - 它在 length() 之后使用
$. 是当前行号
$_ 是当前行

perl -ne 'print length()."  line $.  $_"' myfile | sort -nr | head -n 1

Prints the length, line number, and contents of the longest line

perl -ne 'print length()."  line $.  $_"' myfile | sort -n

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文