如何格式化我的grep输出以在行尾显示行号以及点击数? [英] How can I format my grep output to show line numbers at the end of the line, and also the hit count?

查看:296
本文介绍了如何格式化我的grep输出以在行尾显示行号以及点击数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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.

我该怎么办?

推荐答案

-n返回行号.

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

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

$ grep -in null myfile.txt

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

awk组合以在比赛后打印出行号:

Combine with awk to print out the line number after the match:

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

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

这篇关于如何格式化我的grep输出以在行尾显示行号以及点击数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆