475

I am using grep recursive to search files for a string, and all the matched files and the lines containing that string are printed on the terminal. But is it possible to get the line numbers of those lines too?

Example: presently, I get /var/www/file.php: $options = "this.target", but I am trying to get /var/www/file.php: 1142 $options = "this.target";, well where 1142 would be the line number containing that string.

The syntax I am using to grep recursively is sudo grep -r 'pattern' '/var/www/file.php'

How do we get results for not equal to a pattern? Like all the files, but not the ones having a certain string.

1
  • 2
    The -v flag will show lines that do not match ( grep -v myText MyFile.txt )
    – bcarroll
    Commented Apr 24, 2014 at 13:08

7 Answers 7

712
grep -n SEARCHTERM file1 file2 ...
5
  • 1
    I had another quick question, how do we get results for not equal to a pattern. Like all the files but not the ones having a certain string?
    – sai
    Commented Jul 9, 2010 at 15:23
  • 5
    that would be command line switch -v. If you run 'grep --help' it will display all options
    – Miro A.
    Commented Jul 9, 2010 at 16:05
  • 58
    You don't need -r if you specify multiple files. You only need -r if you specify directories.
    – Sparhawk
    Commented Oct 21, 2015 at 10:10
  • @JeanPaul - I see same result for both -nr and -n -r. What version of grep are you using ? $ grep -n -r name * | wc -l 1984 $ grep -nr name * | wc -l 1984 $ grep -V grep (BSD grep) 2.5.1-FreeBSD
    – Miro A.
    Commented Mar 9, 2018 at 10:47
  • Code only answer, no explanation. :S Commented Apr 2, 2020 at 9:35
187

Line numbers are printed with grep -n:

grep -n pattern file.txt

To get only the line number (without the matching line), one may use cut:

grep -n pattern file.txt | cut -d : -f 1

Lines not containing a pattern are printed with grep -v:

grep -v pattern file.txt
2
  • 4
    And last matching line number: grep -n pattern file.txt | cut -d : -f 1 | tail -1 (you can save this to a variable and use it to e.g tail file from it)
    – Nux
    Commented May 7, 2014 at 14:08
  • And to remove the number/colon at the beginning of each string you can use something like: grep -n pattern file.txt | sed 's/^[0-9][0-9]*://'
    – leetbacoon
    Commented Aug 5, 2019 at 0:02
32

If you want only the line number do this:

grep -n Pattern file.ext | gawk '{print $1}' FS=":"

Example:

$ grep -n 9780545460262 EXT20130410.txt | gawk '{print $1}' FS=":" 
48793
52285
54023
2
  • 5
    awk -F: '{print $1}' also works & you don't need -r in grep when used on files (only directories can be searched recursively) Commented Sep 19, 2017 at 22:49
  • 2
    How to separate awk '{print $1}' FS=":" results and save them in a variable for future manipulations? Commented Aug 2, 2021 at 13:31
13
grep -A20 -B20 pattern file.txt

Search pattern and show 20 lines after and before pattern

4
  • 14
    This is not at all what was asked for. Commented Jul 27, 2015 at 12:25
  • 6
    Also, -C20 is a shorter version if that's what was wanted
    – Marty Neal
    Commented Oct 3, 2017 at 23:36
  • 1
    Lets face it tough - it's an excelent answer for many of us, who google in here while sifting through log files :)
    – Igand
    Commented Sep 14, 2020 at 13:09
  • @JonathonReinhart That may not have been exactly what the OP asked for, but I found it quite useful, and I would encourage folks who answer this type of question to continue adding that sort of tidbit. Commented May 4, 2022 at 12:47
2

grep -nr "search string" directory

This gives you the line with the line number.

2

In order to display the results with the line numbers, you might try this

grep -nr "word to search for" /path/to/file/file 

The result should be something like this:

linenumber: other data "word to search for" other data
0

When working with vim you can place

function grepn() {
    grep -n $@ /dev/null | awk -F $':' '{t = $1; $1 = $2; $2 = t; print; }' OFS=$':' | sed 's/^/vim +/' | sed '/:/s// /' | sed '/:/s// : /'
}

in your .bashrc and then

grepn SEARCHTERM file1 file2 ...

results in

vim +123 file1 : xxxxxxSEARCHTERMxxxxxxxxxx
vim +234 file2 : xxxxxxSEARCHTERMxxxxxxxxxx

Now, you can open vim on the correspondending line (for example line 123) by simply copying vim +123 file1 to your shell.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.