116

I am trying to search my rails directory using grep. I am looking for a specific word and I want to grep to print out the file name and line number.

Is there a grep flag that will do this for me? I have been trying to use a combination of -n and -l but these are either printing out the file names with no numbers or just dumping out a lot of text to the terminal which can't be easily read.

ex:

  grep -ln "search" *

Do I need to pipe it to awk?

codeforester
  • 39,467
  • 16
  • 112
  • 140
trev9065
  • 3,371
  • 4
  • 30
  • 45

5 Answers5

185

I think -l is too restrictive as it suppresses the output of -n. I would suggest -H (--with-filename): Print the filename for each match.

grep -Hn "search" *

If that gives too much output, try -o to only print the part that matches.

grep -nHo "search" * 
Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
spokeadoke
  • 2,000
  • 1
  • 12
  • 5
  • 1
    This answer worked out also for me. I was in need of searching recursively and used this example command: grep -Hnor "localhost" . This listet up all matches with file name and line number, short and concise. – Tore Aurstad Sep 10 '18 at 08:25
  • 1
    This also displays the result and not just the filename and line number – elig Jan 24 '20 at 07:30
34
grep -rin searchstring * | cut -d: -f1-2

This would say, search recursively (for the string searchstring in this example), ignoring case, and display line numbers. The output from that grep will look something like:

/path/to/result/file.name:100: Line in file where 'searchstring' is found.

Next we pipe that result to the cut command using colon : as our field delimiter and displaying fields 1 through 2.

When I don't need the line numbers I often use -f1 (just the filename and path), and then pipe the output to uniq, so that I only see each filename once:

grep -ir searchstring * | cut -d: -f1 | uniq
ib.
  • 27,830
  • 11
  • 80
  • 100
JBW
  • 341
  • 3
  • 3
  • 1
    I feel this best answers the original question. File names and line numbers only. – mastaBlasta Mar 11 '15 at 20:44
  • 1
    Note that this doesn't work if you have absolute path names of Windows like `C:\Users\me\git\` as it already contains one colon. – hirse Nov 20 '15 at 14:34
28

I like using:

grep -niro 'searchstring' <path>

But that's just because I always forget the other ways and I can't forget Robert de grep - niro for some reason :)

Bas van Ommen
  • 1,243
  • 2
  • 12
  • 20
8

The comment from @ToreAurstad can be spelled grep -Horn 'search' ./, which is easier to remember.

grep -HEroine 'search' ./ could also work ;)

For the curious:

$ grep --help | grep -Ee '-[HEroine],'
  -E, --extended-regexp     PATTERNS are extended regular expressions
  -e, --regexp=PATTERNS     use PATTERNS for matching
  -i, --ignore-case         ignore case distinctions
  -n, --line-number         print line number with output lines
  -H, --with-filename       print file name with output lines
  -o, --only-matching       show only nonempty parts of lines that match
  -r, --recursive           like --directories=recurse
Ictus
  • 1,435
  • 11
  • 20
0

Here's how I used the upvoted answer to search a tree to find the fortran files containing a string:

find . -name "*.f" -exec grep -nHo the_string {} \;

Without the nHo, you learn only that some file, somewhere, matches the string.

Karl I.
  • 121
  • 5