Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
If you can afford losing grep colours then becomes as easy as piping it. – uprego 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
up vote 4 down vote accepted

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.

share|improve this answer
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 posting your answer, you agree to the privacy policy and terms of service.

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