23

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

grep CONFIG_PM_ADVANCED_DEBUG /boot/config-`uname -r 

5 Answers 5

41

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

2
  • It is probably worth noting that using -n with -l does not work. -l will take precedence and only the name of the file will be printed. Sep 9, 2021 at 12:07
  • An example in the answer would be helpful!
    – trebor
    Jul 22, 2022 at 1:30
10

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

1
  • mid-air collision with enzotib, who was faster
    – Petr Uzel
    Aug 26, 2011 at 12:20
2

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

This will color the match as well for easy reading.

2

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

0
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
1
  • 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, 2015 at 7:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .