count and number lines of output, useful for counting number of matches

ps aux | grep [h]ttpd | cat -n
If you're on a system that doesn't have nl, you can use cat -n.

2009-12-17 20:45:44

1 Alternatives + Submit Alt

  • Write each FILE to standard output, with line numbers added. With no FILE, or when FILE is -, read standard input. Show Sample Output

    ps aux | grep [a]pache2 | nl
    donnoman · 2009-12-17 18:48:09 1

What Others Think

On debian/ubuntu, nl comes in the same package as cat (coreutils), so it will always be available there. If you just want to count, grep has a count option: ps aux | grep -c [h]ttpd
bwoodacre · 440 weeks and 1 day ago
I'm on MacOS 10.3.9 (darwin/bsd) which doesn't include nl. Looks like it was included as of MacOS 10.4. Thanks, I forgot about grep's line number option. The -c option prints just the line number. The -n option will include the the matched line as well.
putnamhill · 440 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: