grep¶
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- text to search goes inside the double quotes
- nothing is printed in case of no results
- each line containing the searched text is printed
grep -w "text" file-wwill search for whole words- for instance, if two lines contain the text you and yours, and we use
grep -w "you" file, only the line containing you will be returned grep "you" filereturns both the linesprintf "you\nyours\n" | grep "you"printf "you\nyours\n" | grep -w "you"
grep -i- will do a case insensitive search
grep -n- will add a line number as well to the output
printf "you are happy\nhow do you do\nyours truly\n" | grep -n -w "you"
- Multiple flags can be combined together as
-wingrep -winwill match the full word in a case insensitive fashion and print the line numbers int eh result
grep -B num- prints num lines before the match as well so that we can get an idea of the context of the searched word
printf "1\n2\n3\n4\n5\n6\n7\n8\n" | grep -wn -B 2 "5"
grep -A num- will return num lines after the matches
- both the flags can be used in the same query
printf "1\n2\n3\n4\n5\n6\n7\n8\n" | grep -wn -B 2 -A 3 "5"
grep -C num- will return num lines before and after the matches
-C numis equivalent to-A num -B num
Searching multiple files¶
grep "search" ./*- will serch in every file in the current directory
- an error will be returned
- can combine with
findusing the-execflag
grep -r "search" ./*- performs a recursive search within all the directories as well
- in case of many subdirectories/files, recursive search might get too slow
grep -l "search" ./- only returns the list of files where at least one match was found
- a file will appear only once irrespective of the number of matches
grep -c "search" ./- similar to the
-lflag, but will also return the number of matches against each file
- similar to the
grep -r 'pattern' --include='*.txt' --include='*.md' /path/to/dir- searches
patternrecursively, only including specific files - An equivalent expression is
grep -r 'pattern' --include='*.{txt,md}' /path/to/dir
- searches
Piping outputs to grep¶
some command | grep "keyword"- will search the keyword in the output of some command
- multiple
grepcommands can be chained together to form something similar toandfilters
Regular Expressions¶
- By default, grep uses posix compatible regular expressions, while programs like python use perl compatible regular expressions
-Pflag will force grep to use perl compatible regular expressions on linux systems- Mac uses BSD version of grep and does not support the
-Pflag. GNU version of grep, which is used in linux, needs to be installed on Mac to use this flag