我正在使用grep匹配文件中的字符串。下面是一个示例文件:
example one,
example two null,
example three,
example four null,
Grep -i null myfile.txt返回
example two null,
example four null,
如何像这样返回匹配的行及其行号:
example two null, - Line number : 2
example four null, - Line number : 4
Total null count : 2
我知道-c返回总匹配的行,但我不知道如何正确格式化它,在前面添加总空计数,我不知道如何添加行号。
我该怎么办?
-n返回行号。
-i代表忽略大小写。仅在不需要进行大小写匹配时使用
$ grep -in null myfile.txt
2:example two null,
4:example four null,
结合awk输出匹配后的行号:
$ grep -in null myfile.txt | awk -F: '{print $2" - Line number : "$1}'
example two null, - Line number : 2
example four null, - Line number : 4
使用命令替换打印出null总数:
$ echo "Total null count :" $(grep -ic null myfile.txt)
Total null count : 2
-n返回行号。
-i代表忽略大小写。仅在不需要进行大小写匹配时使用
$ grep -in null myfile.txt
2:example two null,
4:example four null,
结合awk输出匹配后的行号:
$ grep -in null myfile.txt | awk -F: '{print $2" - Line number : "$1}'
example two null, - Line number : 2
example four null, - Line number : 4
使用命令替换打印出null总数:
$ echo "Total null count :" $(grep -ic null myfile.txt)
Total null count : 2