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

grep CONFIG_PM_ADVANCED_DEBUG /boot/config-`uname -r 

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

| improve this answer | |

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

| 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.

| improve this answer | |

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

| 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
| 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.