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
How can I format my grep output to show line numbers at the end of the line, and also the hit count?
提问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.txt
returns
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 count
in front, and I don't know how to add the line numbers.
我知道 -c 返回匹配的总行数,但我不知道如何正确格式化以total null count
在前面添加,也不知道如何添加行号。
What can I do?
我能做什么?
采纳答案by dogbane
-n
returns line number.
-n
返回行号。
-i
is 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 awk
to 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 -n
or --line-number
.
使用-n
或--line-number
。
Check out man grep
for lots more options.
查看man grep
更多选项。
回答by jhenninger
use grep -n -i null myfile.txt
to 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
grep
find 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 awk
instead:
或者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君