Part III · Shell and commands
28.2 grep and regex
alsogrepregexregular expressionregexpegreprgripgrepsearch text
TL;DR
grepprints the lines that match a pattern, in files or in what you pipe into it.-rsearches a whole folder,-iignores case,-nshows line numbers,-vkeeps the lines that don't match.-Efor the usual regex syntax (+,?,|,()),-Fto search a plain string.
grep -rn 'TODO' src/
journalctl -b | grep -i 'error'
grep -E 'nginx|php-fpm' services.txt
Pitfalls
- Put the pattern in single quotes, or the shell may change
*,$or!before grep sees it. - Without
-E,|and+match themselves:grep 'a|b'looks for the three charactersa|b. .matches any character:grep '10.0.0.1'also matches10a0b0c1. Use-Ffor a literal string.ps aux | grep nginxalso lists the grep itself:pgrep -a nginxdoesn't.grep -rreads binary files too: add-Ito skip them. It doesn't follow symbolic links;-Rdoes.
Deep dive 2 questions
What does a regex look like?
^ is the start of the line and $ its end. . is any character, [abc]
one of these, [^0-9] anything but a digit. * repeats what comes before
zero or more times. With -E: + once or more, ? optional, {2,4}
between 2 and 4 times, a|b either one, and ( ) groups.
grep -E '^[0-9]{3}-[0-9]{4}$' numbers.txt
grep, egrep or ripgrep?
egrep and fgrep are old names for grep -E and grep -F; since version
3.8, GNU grep warns when you use them. rg (ripgrep, package ripgrep) is
a faster alternative: it searches recursively by default and skips hidden
files, binary files and anything in .gitignore.