grep types
Older Article
This article was published 13 years ago. Some information may be outdated or no longer applicable.
I was chatting with a few colleagues today and they didn’t know there are different types of grep commands under Linux: grep, fgrep, egrep, and pgrep. So here’s a quick breakdown of what each one does.
grep stands for ‘Global Regular Expression Print’ (not a funny spelling of ‘grab’, though that’s a fair assumption). It scans the contents of a file passed in as an argument and returns lines that match your pattern. It takes loads of parameters; I’ll cover the ones I use most.
Let’s say we’ve got a file with the following content:
cat test.txt
Auto-creation of a test room that can have one table
Auto-creation of a test table that can have 2 players
Players need to enter their names to join a table
Basic gameplay is fully implemented but additional rules are missing
If the pack of cards get emptied, the discard pile is reshuffled and will become the new pack
Messages are passed in between the connected clients to indicated end of turn events
Enter the gameplay
grep
grep "enter" test.txt
#returns: Players need to enter their names to join a table
Notice we didn’t get the last line. That’s because grep does case-sensitive matching by default. The -i flag fixes that:
grep -i "enter" test.txt
#returns:
#Players need to enter their names to join a table
#Enter the gameplay
egrep
What if you want to search for ‘enter’ and ‘gameplay’ (an OR condition) in a single pass? That’s where egrep comes in: Extended Global Regular Expressions Print. It treats special characters like |, ?, (, ), + as regex metacharacters. (If you actually want to find a literal pipe symbol, you’d need to escape it with a backslash: \|.)
egrep -i "enter|gameplay" test.txt
#returns:
#Players need to enter their names to join a table
#Basic gameplay is fully implemented but additional rules are missing
#Enter the gameplay
(An alternative would be to run grep -E)
pgrep
This one’s different from the others. It searches running processes to find a process ID (pgrep stands for Process ID Global Regular Expression Print).
Example: pgrep httpd returns the process ID of the Apache webserver.
fgrep
Last but not least: fgrep, usually called ‘fast grep’, officially ‘Fixed string Global Regular Expression Print’ (equivalent of grep -F). It ignores regex metacharacters entirely, which makes it faster. The use case: you’re searching for a character that also happens to be a regex metacharacter. grep "." test.txt returns every single line (because . matches anything in regex). fgrep "." test.txt only returns lines with an actual dot.
Two other flags I reach for regularly: -aX and -bY (where X and Y are integers). These print X lines after and Y lines before the matched line:
grep "fully" -a1 -b1 test.txt
#returns:
#Players need to enter their names to join a table
#Basic gameplay is fully implemented but additional rules are missing
#Enter the gameplay
The -v flag is another favourite. It excludes lines matching a pattern from the results. Say every second line in test.txt is a -1 (maybe another application uses it for line breaks, silly as that sounds). To strip those out:
grep "fully" test.txt | grep -v "\-1"
#returns:
#Basic gameplay is fully implemented but additional rules are missing
#Don't forget we have -1s all over the place in our test.txt now!
We had to escape the - symbol, otherwise grep would treat it as a metacharacter and choke.