Linux Basics – grep with variations
Grep and Friends
In this Linux Basics article, we will have a look at the grep command and its variants, egrep (Extended grep), fgrep (Fixed grep), pgrep (Process grep) and rgrep (Recursive grep). We asked our HOSTAFRICA Linux guru a few questions and this is what he came up with.
Q: What does it do?
A: It finds all lines of text that contain an expression.
Q: Where does a strange name like grep come from?
A: It does seem quite appropriate if we associate it with the word grab, but in truth, it is an abbreviation for Global Regular Expression Print. This is because the grep command uses “Basic Regular Expressions” as well as normal strings as a search pattern. In Basic Regular Expressions (BRE), meta-characters like:
'?' lose their meaning and are treated as normal characters and need to be escaped if they are to be treated as special characters.
Q: How do we use grep?
A: If we had a file called sample.txt containing the following (note the last line is blank):
The quick brown fox jumped over the lazy dog .
Then the following lines show how the grep command may be used:
# Show the line containing an x grep x sample.txt fox # Show all lines with spaces grep ' ' sample.txt The quick jumped over the lazy # Show empty lines grep ^$ sample.txt # Show all non-empty lines (-v turns it into a negative - NOT containing search string) grep -v ^$ sample.txt The quick brown fox jumped over the lazy dog . # Show lines starting with the letter t, but ignore the case ( -i ) grep -i ^t sample.txt The quick the lazy
This is just a small sample of what can be done with just the basic grep command.
Q: What is the egrep command?
A: The egrep command is basically an alias for grep -E or extended grep. This commands allows you to use regular expressions in your search and they will be interpreted as such. With the normal grep, regular expressions are regards as part of the search string unless escaped with a backslash ( \ ).
Here are a few examples using egrep.
# Show lines containing 'x' or 'z' egrep 'x|z' sample.txt fox the lazy # Show lines containg the word 'the', with the options of a capital or lowercase 't' egrep [Tt]he sample.txt The quick the lazy # a longer way to do the same as above egrep 'The|the' sample.txt The quick the lazy
Q: And the fgrep command?
A: This should actually be the default for anyone starting to learn these commands. The fgrep command is an alias for grep -F and is called fixed grep. This version of grep will ignore any strange expressions and ALWAYS search for exactly the string you ask it to find. Thus fgrep [Tt]he sample.txt will search for the text “[Tt]he” in the sample file. This search then gives us a zero result as this string does not exist in the sample.txt file.
Q: What about the rgrep command
A: This command is an alias for grep -r. It is called recursive grep. This is useful to find all files containing an expression. Here is an example using our sample.txt file:
grep -r "the lazy" ./*.txt ./sample.txt:the lazy
Q: Does the pgrep command fit in here?
A: No. This is not a, strictly speaking, part of the grep family as it forms part of the process control command set. The pgrep command is used to return a PID or Process ID of a process that contains the search string in its name.
In our next exciting Linux Basics article, we will chat about the sed command and see what we can say about it!