When we're filtering the output with grep, is there any way we can get line numbered output, like we get with cat -n? I'm not asking for the grep -n option which shows the line number of the match in the file.

The scenario I want to use it in is that when I download multiple files using aria2, I usually filter the output with keyword NOTICE which gives me some output every time a file download is completed. But I have actually no way of knowing how many files have been downloaded.

  • If you can afford losing grep colours then becomes as easy as piping it. – 41754 Aug 13 '13 at 6:42
  • Losing colors is fine but I actually wanted the filtered output to be line-numbered. Using nl solves this. – vknha Aug 13 '13 at 12:34

You can try something like

grep PATTERN FILE | nl


grep PATTERN FILE | wc -l

The first one will number the filtered lines. The second one will count them all.

  • 1
    Thanks for this, nl works. I also had to add --line-buffered to grep since I was filtering out data from a continuous stream. – vknha Aug 13 '13 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.