Familiarity with grep is a must for aspiring Linux systems administrators.
In my Linux lexicon, grep ranks among the most useful command line tools.
So why is grep such a useful tool?
Grep (which stands for global regular expression print) lets users find text or other patterns in files.
The good thing is that grep usually comes pre-installed with most Linux distributions.
Although grep might not be as frequently used as as cp or ls, it ranks among the top commands for systems administrators.
Grep – Syntax
The basic syntax for the oft-used grep command is:
grep [OPTIONS] PATTERN [FILE…]
When grep finds the pattern, it prints the lines containing the specified search string or pattern.
Your search expression could be text, numbers or metacharacters.
Let me illustrate the use of grep with some examples.
Here’s how you’d use grep to find the word “salmon” in a long recipe file.
bobby@centos-gal $ grep salmon Myrecipe-file.txt
Now that was a basic illustration of how we can use grep.
Let’s now consider a slightly more complex example involving grep.
When you’re analyzing Apache error_logs of a RedHat or any other Linux server, you might want to check the total number of wp-login attacks (a serious issue that WordPress bloggers encounter).
bobby@centos-gal $ tail -500 /var/log/httpd/error_log | grep -w wp-login -c 85
Grep – Various Options
Grep has several options that expand its capabilities.
Let’s examine a few of the key ones.
Case Insensitive Search
With the below -i option, you can do a case insensitive search:
bobby@centos-gal $ grep -i "search_string" File_Name
You can also use grep to print out lines that do not match a pattern via the -v option.
This is known as invert-search and your output will show only non-matching lines.
bobby@centos-gal $ grep -v "search_string" File_Name
Let’s say you want to search a string in the current directory along with all its sub-directories, then you must specify the –r option to search recursively:
bobby@centos-gal $ grep –r “search_string” /directory_name
In this example, you are looking at the total number of matches for your search string and not the lines per se.
bobby@centos-gal $ grep -c "search_string" File_Name
Consider a scenario where you are interested in printing only the names of files containing a match to your search string instead of the matches themselves.
In such a scenario, you’d use the option -l.
bobby@centos-gal $ grep -l "search_string" File_Names
No Match Files
Unlike in the previous example, we use the upper case -L option to get the names of files that don’t match the search string.
bobby@centos-gal $ grep -L "search_string" File_Names
You must use option -n if you wish to prefix the line number to the matching line in the file.
bobby@centos-gal $ grep -n "string" File_Name
Supress File Name Output
Consider option -h when you want to suppress file-name output in your results.
You’ll just get the matches of the search string in the output.
bobby@centos-gal $ grep -h "search_string" File-Names
It’s a testament to grep’s versatility that we can even use it with metacharacters like the dot (the period character on your keyboard) to match any character.
bobby@centos-gal $ grep -h '.juice' Myrecipes.txt
For instance, in the above example the grep search will pull up applejuice, grapejuice, limejuice, pomegranatejuice and tomatojuice from the recipes in the Myrecipes.txt file.
In the above post, we’ve considered only the tip of the grep iceberg.
There’s much more to grep.
To get a solid grip on grep and its various options, I encourage you to check out the man pages using man grep on the command line.