Grep

·

1 min read

Grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.

It's name comes from the ed command g/re/p (global / regular expression search / and print), which has the same effect


To display all the lines in the file that have the matching string inside them

grep "<string>" <file_name>
grep -i "<string>" <file-name>

To display the line numbers

grep -n "<string>" <file_name>

To search for multiple strings

grep -e "<string>" -e "<string>" <file_name>

To display all the file names that have the matching string inside them

grep -lR "<string>"<file_name>

To print only those lines that start with the matching string

grep "^<string>" <file_name>

To print only those lines that end with the matching string

  grep "$<string>" <file_name>

To print the number of lines that have the matching string

grep -c "<string>" <file_name>

To print all the lines that do not have the matching string

grep -v "<string>" <file_name>