Sign up ×
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.

How to display line number while doing grep on a file. For example:

grep CONFIG_PM_ADVANCED_DEBUG /boot/config-`uname -r 
share|improve this question

5 Answers 5

up vote 24 down vote accepted

There is the option -n, and many more in the manual page, worth reading.

share|improve this answer
thanks ... i was going thru man grep. – user7044 Aug 26 '11 at 12:16

grep -n <pattern> <file> prefixes each line of output with the line number in the input file. Is this what you are looking for?

share|improve this answer
mid-air collision with enzotib, who was faster – Petr Uzel Aug 26 '11 at 12:20

alias grep='grep -inE --color=auto'

This will color the match as well for easy reading.

share|improve this answer

Have you checked cat -n 'filename'. This will print the line numbers.

share|improve this answer
cat -n /boot/config | grep CONFIG_PM_ADVANCED_DEBUG

cat will -n[umber] the lines and | (filter) through grep looking only for lines with CONFIG_PM.... in them

the resulting output will be line number; the text of the line and the string

cat -n[umber lines] /Path/to/filename | grep -i[gnor case (optional)] STRING_TO_LOOK_FOR
share|improve this answer
Welcome to U&L. I edited your post, so it utilizes the formatting possibilities of the site a bit more. Please review the changes (with the edit history), so your future answers (and questions) might look better. It is also not necessary to "sign" your post, your name is posted with your profile on all answers – Anthon Oct 12 at 7:33

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.