grep
grep
stands for global rgular expression and print and allows us to search for text within files.
grep
does not work with directories. If something like grep "search" ./
is tried, an error is returned.
grep "text to search" file_name
grep -w "text" file
-w
will search for whole wordsgrep -w "you" file
, only the line containing you will be returnedgrep "you" file
returns both the linesprintf "you\nyours\n" | grep "you"
you
yours
printf "you\nyours\n" | grep -w "you"
you
grep -i
grep -n
printf "you are happy\nhow do you do\nyours truly\n" | grep -n -w "you"
1:you are happy
2:how do you do
-win
grep -win
will match the full word in a case insensitive fashion and print the line numbers int eh resultgrep -B num
printf "1\n2\n3\n4\n5\n6\n7\n8\n" | grep -wn -B 2 "5"
3-3
4-4
5:5
grep -A num
printf "1\n2\n3\n4\n5\n6\n7\n8\n" | grep -wn -B 2 -A 3 "5"
3-3
4-4
5:5
6-6
7-7
8-8
grep -C num
-C num
is equivalent to -A num -B num
grep "search" ./*
find
using the -exec
flaggrep -r "search" ./*
grep -l "search" ./
grep -c "search" ./
-l
flag, but will also return the number of matches against each filegrep
some command | grep "keyword"
grep
commands can be chained together to form something similar to and
filters-P
flag will force grep to use perl compatible regular expressions on linux systems-P
flag. GNU version of grep, which is used in linux, needs to be installed on Mac to use this flag