Use grep to report back only line numbers

Neil picture Neil · Aug 5, 2011 · Viewed 100.3k times · Source

I have a file that possibly contains bad formatting (in this case, the occurrence of the pattern \\backslash). I would like to use grep to return only the line numbers where this occurs (as in, the match was here, go to line # x and fix it).

However, there doesn't seem to be a way to print the line number (grep -n) and not the match or line itself.

I can use another regex to extract the line numbers, but I want to make sure grep cannot do it by itself. grep -no comes closest, I think, but still displays the match.

Answer

love_me_some_linux picture love_me_some_linux · Aug 5, 2011

try:

grep -n "text to find" file.ext | cut -f1 -d: