尝试使用行号来获取最大行的文件

时间:2023-01-03 09:43:22

So i've been goofing with this since last night and I can get a lot of things to happen just not what I want.

所以自从昨晚以来我一直在玩这个问题,我可以做很多事情而不是我想要的。

I need a code to find the file with the most lines in a directory and then print the name of the file and the number of lines that file has.

我需要一个代码来查找目录中包含最多行的文件,然后打印文件名和文件所包含的行数。

I can get the entire directory's lines to print but can't seem to narrow the field so to speak.

我可以打印整个目录的行,但似乎无法缩小字段。

Any help for a fool of a learner?

对傻瓜学习者有什么帮助吗?

wc -l $1/* 2>/dev/null
   | grep -v ' total$'
   | sort -n -k1
   | tail -1l

After some pro help in another question, this is where I got to, but it returns them all, and doesn't print their line counts.

在另一个问题的一些亲帮助之后,这就是我所要做的,但它会全部归还,并且不会打印它们的行数。

2 个解决方案

#1


1  

Following awk command should do the job for you and you can avoid all redundant piped commands:

遵循awk命令应该为您完成工作,您可以避免所有冗余的管道命令:

wc -l $1/* | awk '$2 != "total"{if($1>max){max=$1;fn=$2}} END{print max, fn}'

UPDATE: To avoid last line of wc's output this might be better awk command:

更新:为了避免wc输出的最后一行,这可能是更好的awk命令:

wc -l $1/* | awk '{arr[cnt++]=$0} END {for (i=0; i<length(arr)-1; i++) 
                  {split(arr[i], a, " "); if(a[1]>max) {max=a[1]; fn=a[2]}} print max, fn}'

#2


0  

you can try:

你可以试试:

wc -l $1/* | grep -v total | sort -g | tail -1

actually to avoid the grep that would also remove files containing "total":

实际上要避免grep也会删除包含“total”的文件:

for f in $1/*; do wc -l $f; done | sort -g | tail -1

or even better, as suggested in comments:

甚至更好,正如评论中所建议的那样:

wc -l $1/* | sort -rg | sed -n '2p'

you can even make it a function:

你甚至可以使它成为一个功能:

function get_biggest_file() {
    wc -l $* | sort -rg | sed -n '2p'
}

% ls -l
...  0 Jun 12 17:33 a
...  0 Jun 12 17:33 b
...  0 Jun 12 17:33 c
...  0 Jun 12 17:33 d
... 25 Jun 12 17:33 total
% get_biggest_file ./*
5 total

EDIT2: using the function I gave, you can simply output what you need as follows:

EDIT2:使用我给出的功能,您可以简单地输出您需要的内容,如下所示:

get_biggest $1/* | awk '{print "The file \"" $2 "\" has the maximum number of lines: " $1}'

EDIT: if you tried to write the function as you've written it in the question, you should add line continuation character at the end, as follows, or your shell will think you're trying to issue 4 commands:

编辑:如果您在问题中编写函数时尝试编写函数,则应在末尾添加行继续符,如下所示,或者您的shell会认为您尝试发出4个命令:

wc -l $1/* 2>/dev/null \
   | grep -v ' total$' \
   | sort -n -k1 \
   | tail -1l

#1


1  

Following awk command should do the job for you and you can avoid all redundant piped commands:

遵循awk命令应该为您完成工作,您可以避免所有冗余的管道命令:

wc -l $1/* | awk '$2 != "total"{if($1>max){max=$1;fn=$2}} END{print max, fn}'

UPDATE: To avoid last line of wc's output this might be better awk command:

更新:为了避免wc输出的最后一行,这可能是更好的awk命令:

wc -l $1/* | awk '{arr[cnt++]=$0} END {for (i=0; i<length(arr)-1; i++) 
                  {split(arr[i], a, " "); if(a[1]>max) {max=a[1]; fn=a[2]}} print max, fn}'

#2


0  

you can try:

你可以试试:

wc -l $1/* | grep -v total | sort -g | tail -1

actually to avoid the grep that would also remove files containing "total":

实际上要避免grep也会删除包含“total”的文件:

for f in $1/*; do wc -l $f; done | sort -g | tail -1

or even better, as suggested in comments:

甚至更好,正如评论中所建议的那样:

wc -l $1/* | sort -rg | sed -n '2p'

you can even make it a function:

你甚至可以使它成为一个功能:

function get_biggest_file() {
    wc -l $* | sort -rg | sed -n '2p'
}

% ls -l
...  0 Jun 12 17:33 a
...  0 Jun 12 17:33 b
...  0 Jun 12 17:33 c
...  0 Jun 12 17:33 d
... 25 Jun 12 17:33 total
% get_biggest_file ./*
5 total

EDIT2: using the function I gave, you can simply output what you need as follows:

EDIT2:使用我给出的功能,您可以简单地输出您需要的内容,如下所示:

get_biggest $1/* | awk '{print "The file \"" $2 "\" has the maximum number of lines: " $1}'

EDIT: if you tried to write the function as you've written it in the question, you should add line continuation character at the end, as follows, or your shell will think you're trying to issue 4 commands:

编辑:如果您在问题中编写函数时尝试编写函数,则应在末尾添加行继续符,如下所示,或者您的shell会认为您尝试发出4个命令:

wc -l $1/* 2>/dev/null \
   | grep -v ' total$' \
   | sort -n -k1 \
   | tail -1l