Anyone who’s administered a Linux server for a certain amount of time is familiar with the “grep” command. With Linux’s philosophy of “Everything is a file”, grep becomes crucial to traversing log files, configuration files, checking for certain errors in output files, and getting the number of matches.
|
|
Searching for Multiple Words at the Same Time
Normally, the usage for grep goes something like this:
grep 'pattern' filename
Where ‘pattern’ is any given pattern or regular expression to match. However, let’s say you have an error log file and are performing routine maintenance. You want to check the log files for the existence of some keywords that span a variety of sources. Normally, you’d have to use a separate grep expression for each of these phrases.
However, we can use regular expressions easily with the “-E” parameter. To start with, we have a file called “animal-count” with the following contents:
grep pattern filename
Let’s say we want to search this file for these two phrases:
sparrow
dogs
We can use the following grep expression:
grep -Ew 'sparrow|dogs' animal-count
There are two flags:
-E means that we’re using extended regular expressions
-w means that we only want to match whole words
To make it even easier, we can just replace “grep -E” with “egrep”. So the above command becomes:
egrep -w 'sparrow|dogs' animal-count
And here’s the output:
egrep -w -count
We can match as many words as we need to that way. If you want to remove the “words” requirement and instead just want a straight up string match, then you can drop the “w” parameter.
Searching an Entire Directory for a Match and Showing the File Name
We normally think of grep searching either a file or a specific output from another command. However, we can also use it to search an entire directory of files at the same time.
Let’s say for example, that you have a bunch of log files scattered all around the system. After all, many packages have their own locations for log files. To make things easy for you, you create a new folder containing symbolic links to all the various log files that are of interest to you.
Now you can search all of them together with grep using the “-R” command. The capital “R” tells grep to include symbolic links. The small “r” ignores symbolic links not found on the command line by default.
For example, in the following example, we search the current directory for a string:
grep -r sparrow .
The dot (.) at the end indicates the current directory. We get the following output:
grep -r
You can see below, that it searches recursively as well with another pattern match:
Recursive file searching with grep -r
This is very useful for searching a bunch of files at the same time. The output also shows you the name of the file containing the matching strings! I personally like this solution with “-R” for searching through a set of symbolic links at once, to save time.
Counting the Number of Matching Lines
Sometimes you want to know how many times a certain keyword has appeared in grep. For example, if you’re searching the website files for a certain spider, you want a count of the number of lines containing a specific user agent.
To do that, we simply add in the “-c” parameter. Keep in mind that this removes the regular output of grep and it will no longer show you individual matches. For example, without the “-c” parameter:
grep rr animal-count
We get a simple list of occurrences. With “-c” however:
grep -c rr animal-count
And we get a count of the number of lines:
grep command counting the number of matching lines
As you can see, it no longer displays each occurrence.
Grep is an extremely powerful tool, and its functionality has only increased over the years. We can create complicated regular expressions, and even create an expression to format the output of a file to make it easier to read. These examples of advanced grep usage will give you a taste of what’s possible, and will hopefully encourage you to delve into the manual yourself!
If you are one of our fully managed web hosting customers, you don’t have to come up with complex grep commands yourself, you can simply ask our system administrators to help you with grep commands. They are available 24/7 and will take care of your request immediately.
If you liked this post on advanced grep commands, please share it with your friends via social media networks. If you have any questions regarding this tutorial, please write a comment below. Thanks!
FacebookTwitterGoogle+LinkedIn
CUSTOMIZE
SHARE:
PREVIOUSHow to Install Nextcloud 14 on Debian 9
RELATED POSTS
How to Set Up Private DNS Servers with BIND on Ubuntu 16.04
How to Set Up Private DNS Servers with BIND on Ubuntu 16.04
November 21, 2017
ERR_SSL_VERSION_OR_CIPHER_MISMATCH
ERR_SSL_VERSION_OR_CIPHER_MISMATCH
May 22, 2018
Guide to Linux Environment Variables
Guide to Linux Environment Variables
May 21, 2018
How to Create Custom Commands in Linux
How to Create Custom Commands in Linux
June 28, 2018
Leave a reply
Logged in as admin. Log out?
COMMENT
COMMENT
Server Management Services
Full Story |