Regular Expressions in the Unix Shell - The grep command
(Page 4 of 4 )
The grep command is what will take the regular expression we just learned on the previous page, and try to match it in an input file. The input comes through the standard input, so if we want to search in a file we will use the cat command and a pipe. If we want to perform the search on a specific text, we use the echo and the pipe. Additionally, you can get the source from any other place.
The grep command will automatically set the $? to true if a valid match is found, or false otherwise. By default, grep will print back the lines in which it managed to make a match. If multiple matches are possible, the line will be printed only once. If the GREP_OPTIONS has the -color=auto specified, the command will colorize the matches.
To set this as a default behavior of the command, you need to add the following line at the end of the .bashrc file in the home folder:
export GREP_OPTION='-color=auto'
The table below will enumerate the options possible for the grep command:
Option
What it does
-c
Count the number of matches and return the result
-E
Work with the extended regular expressions
-e pattern
We can define the pattern this way also; otherwise, it will take the first text argument as the pattern
-P
Work with Perl regular expressions
-f file
Use the file specified instead of the standard input
-i
Ignore differences between capital and lower case letters
-L
In the case of multiple files, return the one in which there is not a match
-l
In the case of multiple files return the one in which there is a match
-m number
After m number of matches, stop
-n
Before the match, print the number of matches
-o
Print only the matches
-q
Do not print anything. Just set the $? special variable if there was or not a match
-r or -R
Search recursively in the given directory
-s
Do not print an error message for those files that the command cannot read
-v
Print the lines in which there was no match
-w
A match is considered correct only if there is a single word, or more precisely the pattern is surrounded by the b character.
-x
Only matches forming a full line (^pattern$)
The grep command uses a greedy matching algorithm, meaning that it will try to match the pattern to the longest sequence possible. For example, with the "a.*b" pattern, on the following text, the match will be:
aaabbbbaaaaabbbbbba
Finally, a couple of concrete examples:
if echo "$variable" | grep -E '^Yeti' >/dev/null 2>&1
then
echo The variable starts with Yeti
fi
if cat alfa| grep -q -E '^alfa$'
then
echo alfa found in the file alfa in a single line
fi
if file "$variable" | grep -q -E "text"
then
echo The "$variable" is a text file
fi
There could be countless more examples. You can create a pattern to find all the dates or to find all the positive numbers, for instance. I will leave to you play around with this inside the terminal. Before you will do this, please rate my article and post your questions on the blog following this article. Next time I will present the sed command, which stands for stream editor. Please make sure you come back. Live With Passion!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.