global, regular expression, print
The grep command is one of those fundamental commands.
We’ve already gotten a sneak peek of grep on Dec 8, as it is hard to
come up with good examples for pipes without it.
Grep is short for “global, regular expression, print”.
Where the find command allows you to search for files, then grep searches
in text.
You might have heard about regular expression if you’ve done programming in another language. Because most languages supports it in some form.
Let’s create a file with some text, so we have something to use as input.
cat > unix-philosophy.txt << EOF
Write programs that do one thing and do it well.
Write programs to work together.
Write programs to handle text streams, because that is a universal interface.
EOF
We can then use grep look for the word “text”.
cat unix-philosophy.txt | grep text
Here, grep searches through the text it gets from stdin.
This similar to how we saw it used on Dec 8 with lshw.
If we are only interested in searching for text in a file, then we can just
pass the filename to grep directly.
grep text unix-philosophy.txt
Another common use case for grep is to look for processes.
ps aux | grep bash
Where ps aux list all running processes.
This is so common that some systems provide a combined command for it called
pgrep.
pgrep -a bash
Does (almost) the same.
Regular expression
What about the regular expression part? We can use grep to search for a pattern, instead of searching an exact sub-string.
grep th. unix-philosophy.txt
Where “.” matches any single character.
It can also match on lines ending with something.
grep 'er\.$' unix-philosophy.txt
Where $ means the end of a line.
Notice \. because otherwise it would also match lines ending with “er?”.
We can also use ^ for beginning.
grep '^Write' unix-philosophy.txt
Not very interesting as all lines start with the same.
When grepping for patterns, I like to use grep -P because it makes it more
consistent with the regular expressions in other programming languages.
Let’s add another line to our file.
echo "This is the UNIX-philosophy." >> unix-philosophy.txt
Say we want all lines that contain “this” or “that”, we can use |.
grep -P -i "this|that" unix-philosophy.txt
The -i flag makes it case-insensitive.
You can learn more about Perl style regex here.