Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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. –  galegosimpatico 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. –  vikan Aug 13 '13 at 12:34
add comment

1 Answer

up vote 3 down vote accepted

You can try something like

grep PATTERN FILE | nl

or

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. –  vikan Aug 13 '13 at 8:32
add comment

Your Answer

 
discard

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.