Linux 如何格式化我的 grep 输出以在行尾显示行号以及命中计数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3968103/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:44:56  来源:igfitidea点击:

How can I format my grep output to show line numbers at the end of the line, and also the hit count?

linuxbashunixgrep

提问by London

I'm using grep to match string in a file. Here is an example file:

我正在使用 grep 来匹配文件中的字符串。这是一个示例文件:

example one,
example two null,
example three,
example four null,

grep -i null myfile.txtreturns

grep -i null myfile.txt返回

example two null,
example four null,

How can I return matched lines together with their line numbers like this:

如何返回匹配的行及其行号,如下所示:

  example two null, - Line number : 2
  example four null, - Line number : 4
  Total null count : 2

I know -c returns total matched lines, but I don't how to format it properly to add total null countin front, and I don't know how to add the line numbers.

我知道 -c 返回匹配的总行数,但我不知道如何正确格式化以total null count在前面添加,也不知道如何添加行号。

What can I do?

我能做什么?

采纳答案by dogbane

-nreturns line number.

-n返回行号。

-iis for ignore-case. Only to be used if case matching is not necessary

-i用于忽略大小写。仅在不需要大小写匹配时使用

$ grep -in null myfile.txt

2:example two null,
4:example four null,

Combine with awkto print out the line number after the match:

结合使用awk以打印出匹配后的行号:

$ grep -in null myfile.txt | awk -F: '{print " - Line number : "}'

example two null, - Line number : 2
example four null, - Line number : 4

Use command substitution to print out the total null count:

使用命令替换打印出总空计数:

$ echo "Total null count :" $(grep -ic null myfile.txt)

Total null count : 2

回答by Andy Lester

Use -nor --line-number.

使用-n--line-number

Check out man grepfor lots more options.

查看man grep更多选项。

回答by jhenninger

use grep -n -i null myfile.txtto output the line number in front of each match.

用于grep -n -i null myfile.txt输出每个匹配项前的行号。

I dont think grep has a switch to print the count of total lines matched, but you can just pipe grep's output into wc to accomplish that:

我不认为 grep 有一个开关来打印匹配的总行数,但你可以将 grep 的输出通过管道传输到 wc 来完成:

grep -n -i null myfile.txt | wc -l

回答by ghostdog74

grepfind the lines and output the line numbers, but does not let you "program" other things. If you want to include arbitrary text and do other "programming", you can use awk,

grep查找行并输出行号,但不允许您“编程”其他内容。如果要包含任意文本并进行其他“编程”,可以使用 awk,

$ awk '/null/{c++;print 
c=0
while read -r line
do
  case "$line" in
   *null* )  (
    ((c++))
    echo "$line - Line number $c"
    ;;
  esac
done < "file"
echo "total count: $c"
," - Line number: "NR}END{print "Total null count: "c}' file example two null, - Line number: 2 example four null, - Line number: 4 Total null count: 2

Or only using the shell(bash/ksh)

或者只使用 shell(bash/ksh)

awk '/null/ { counter++; printf("%s%s%i\n",
perl -npe 'chomp; /null/ and print "$_ - Line number : $.\n" and $i++;$_="";END{print "Total null count : $i\n"}'
, " - Line number: ", NR)} END {print "Total null count: " counter}' file

回答by Zsolt Botykai

Or use awkinstead:

或者awk改用:

grep -irn "YourStringToBeSearch"

回答by hannes

or in perl (for completeness...):

或在 perl 中(为了完整性......):

2:example two null,  
3:example three,  
4:example four null,   

回答by Vrushal Raut

Refer this link for linux command linux http://linuxcommand.org/man_pages/grep1.html

请参阅此链接以获取 linux 命令 linux http://linuxcommand.org/man_pages/grep1.html

for displaying line no ,line of code and file use this command in your terminal or cmd, GitBash(Powered by terminal)

要显示行号、代码行和文件,请在您的终端或 cmd、GitBash(由终端提供支持)中使用此命令

##代码##

回答by Honest Abe

Just thought I'd something that might help you in the future. To search multiple string and output line numbers and browse thru the output, type:

只是想我会在将来对您有所帮助。要搜索多个字符串和输出行号并浏览输出,请键入:

egrep -ne 'null|three'

egrep -ne 'null|three'

will show:

将会呈现:

##代码##

egrep -ne 'null|three' | less

egrep -ne 'null|three' | less

will display output in a less session

将在较少的会话中显示输出

HTH Jun

HTH君