This list is by no means complete and only aims to display some common usages of the grep
command.
man find
grep -V
Note By default, Linux
will have the GNU grep whereas macOS will have the BSD grep.
grep -w "John" file.txt
will look for the word John in file.txt
.
grep -wi "John doe" file.txt
will perform a case insensitive search for John Doe.
grep -win "doe" file.txt
will perform a case insensitive for doe in the file and display the results along
with the corresponding line numbers from file.txt
.
grep -winr "doe"
will perform a case insensitive recursive search for doe and display the
results with line numbers.
grep -wirl "doe"
will perform a case insensitive recursive search for doe and display only the file names that
contain the search text.
grep -wirc "doe" file.txt
will display the files containing the search string along with the number of hits found in
each file.
grep -winv "Jane" file.txt
will display all the lines not containing Jane.
grep -wA 2 "jane" file.txt
will display the two lines from the file that follow the lines containing jane.
grep -wB 2 "jane" file.txt
will display the two lines from the file that precede the lines containing jane.
grep -wC 2 "jane" file.txt
will display the two lines from the file that precede and follow the lines containing jane.
grep "...-...-....
file.txt` will display strings having the format abc-efg-hijk or 123-456-7890.
Below command will work for GNU grep
only
grep -P "\d{3}-\d{3}-\d{4}" file.txt
will display numbers having the format 123-456-7890.