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

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


-n returns line number.

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

$ grep -in null myfile.txt2:example two null,4:example four null,

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 : 2example 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


Use -n or --line-number.

Check out man grep for lots more options.


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

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 -n -i null myfile.txt | wc -l