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

383,926

Solution 1

-n returns line number.

-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,

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

Solution 2

Use -n or --line-number.

Check out man grep for lots more options.

Solution 3

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

Solution 4

Or use awk instead:

awk '/null/ { counter++; printf("%s%s%i\n",$0, " - Line number: ", NR)} END {print "Total null count: " counter}' file

Solution 5

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,

$ awk '/null/{c++;print $0," - 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)

c=0
while read -r line
do
  case "$line" in
   *null* )  (
    ((c++))
    echo "$line - Line number $c"
    ;;
  esac
done < "file"
echo "total count: $c"
Share:
383,926
London
Author by

London

Jeanie Taylor

Updated on July 04, 2020

Comments

  • London
    London almost 4 years

    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 returns

    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.

    What can I do?

  • London
    London over 13 years
    can I format this by adding line numbers after instead of before with :?
  • dpatchery
    dpatchery over 13 years
    -c will print the total lines matched
  • jhenninger
    jhenninger over 13 years
    You are right. Unfortunately it also suppresses normal output.
  • London
    London over 13 years
    your solution seems fine but get an error awk95: syntax error at source line context is >>> ' <<< missing } awk95: bailing out at source line 1
  • London
    London over 13 years
    sorry switched to linux now its working :) it was windows version not so good
  • Dzung Nguyen
    Dzung Nguyen almost 12 years
    new linux user is lazy of reading man page. But if they use linux enough, they will be used to it :) It's super useful :)
  • TecBrat
    TecBrat over 10 years
    Not always lazy, (But sometimes), often it is that a new Linux user has trouble understanding a man page. (They can seem cryptic)
  • Eugen Konkov
    Eugen Konkov almost 6 years
    sometimes man page can take many pages. And this is hard to read all of them
  • santiago arizti
    santiago arizti almost 6 years
    ...the knights who say -ni thats how you remember this trick