Skip to content
Advertisement

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

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

JavaScript

grep -i null myfile.txt returns

JavaScript

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

JavaScript

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.

What can I do?

Advertisement

Answer

-n returns line number.

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

JavaScript

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

JavaScript

Use command substitution to print out the total null count:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement