Get line number of all matches in a file

awk '/match/{print NR}' file

By: inof
2009-06-03 17:34:13

What Others Think

since cat -n numbers its lines, an alternative would be grep 'match' file | cat -n
spatz · 613 weeks and 2 days ago
The grep | cat solution will number the found lines while the awk solution will show line number from the full file.
sthillay · 613 weeks and 1 day ago
grep numbers lines on its own, for something similar you could do: grep -n match file For example: grep -n char test.cpp 6:int main (int argc, char **argv) 8: char description[256]; 9: char country[50]; 10: char category[20]; 11: char id[6]; 12: char pack[10]; Or pipe it to cut to snip out JUST the line numbers: grep -n char test.cpp |cut -f 1 -d ':' 6 8 9 10 11 12 (But for just line numbers piping to cut might actually be more work)
hk0i · 613 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: