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.

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

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
up vote 25 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 '15 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.