Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get line number while using grep

Tags:

grep

line

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

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

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

One more question is, how do we get results for not equal to a pattern. Like all the files but not the ones having a certain string?

like image 721
sai Avatar asked Jul 09 '10 14:07

sai


People also ask

How do I get line numbers from a file?

Use grep -n string file to find the line number without opening the file.

How do I show line numbers in Linux?

Using the wc -l command is the most used and also the easiest way to find line numbers of a given file.

How do I print line numbers in Unix?

Press the Esc key if you are currently in insert or append mode. Press : (the colon). The cursor should reappear at the lower left corner of the screen next to a : prompt. A column of sequential line numbers will then appear at the left side of the screen.


1 Answers

grep -n SEARCHTERM file1 file2 ... 
like image 181
Miro A. Avatar answered Sep 20 '22 17:09

Miro A.