How to Efficiently Use the Unix Grep Command by Examples
Much of the power of unix comes from the great GNU programs. They are actually a very powerful set of tools that are able to help every user or administrator do many things that could otherwise be quite a bit difficult. If you are not really that knowledgeable of Unix and its basic commands, i would suggest that you take a look at this introduction to unix commands.
This post will be about one of the greatest unix commands, the well known grep.As i find it much easier to learn by examples myself, i would be giving you examples of using this command efficiently.
What is grep ?
Grep is a unix tool that is used for printing lines matching a certain pattern. In simple words, think of yourself needing to find a certain string inside a file. This scenario will certainly come up lots of times in your linux/bsd/mac or whatever unix box you use. Below i will be showing you examples of usage based on real time scenarios :
Finding the Lines Where a Certain Phrase Appears
Let’s suppose that we have a file named “new.txt” that has nine lines with the following content:
line1 line2 line3 line4 line5
In order to find the lines of content that contain the text “line”, we just do a simple cat (meaning a simple printout of the file contents) and then pipe that to the grep command. Grep will take that file’s contents as input and check for the text “line” and print out the appropriate ones:
oddy:Desktop thought$ cat new.txt | grep line line1 line2 line3 line4 line5
As you can imagine, since every line contains the text “line” along with a number from 1-5, we get all lines back. If we were to search for the text “line2″ :
oddy-2:Desktop thought$ cat new.txt | grep line2 line2
Printing Lines Before && After The Found Text
Sometimes it is useful that we find just the actual line where the text we are looking for is located, but most times we would really need to look at some lines right before or after this actual line. For instance, we may want to find something from a certain manpage. Once we find that spot, we need to read more than one line, like 5 times before the actual searched text and 5 lines after too.
Luckily, grep has two important switches that do that for us. They are the switches -A and -B. An important principle that you have to utilize while remembering switches, is connecting them to actual words. What i do to remember these two ones is thinking of -A as After and -B as Before. So, -A would be the number of lines that we want to print AFTER the found text and -B the number of lines that we want to print BEFORE the found text.
Therefore, suppose that in the previous example, we needed to find the text “line2″ but also check on the previous 2 lines before that and the 2 lines after that :
oddy-2:Desktop thought$ cat new.txt | grep -A 2 -B 2 line2 line1 line2 line3
You see that 2 lines are printed before the actual searched text and also 2 lines are printed after that (mind that there are empty lines in this txt file too). Also note that the switch -C does the same for you too. So the equivalent of grep -A 2 -B 2 is just the command grep -C 2.
Printing The Number Of Lines Found
Some times you just need to print out the number of occurrences for a certain string to find. You can do this using the switch -c :
oddy-2:Desktop thought$ cat new.txt | grep -c line 5
As you see, there are 5 lines of text in the file “new.txt” where you can find at least one occurrence of the text “line”.
Finding Text Using Regular Expressions
Regular Expressions are a vast area of study by itself. In this tutorial i won’t go into detail about how to use regular expressions. I will just state that they are a specify a pattern of text to be found. For instance, let’s suppose that we only need to find line1, line2, line3 and not line4 and line5. We would have to run grep using a regular expression to do that. For instance, check out the following two examples :
oddy-2:Desktop thought$ cat new.txt | grep -e line line1 line2 oddy-2:Desktop thought$ cat new.txt | grep -e line[1-3] line1 line2 line3
You see that we use the switch -e to do this. The text specified is like “line“. This is actually a regular expression that informs grep that you need to find those lines where there is text like “line” followed by the character 1 or 2. In the second example, the same happens with character from 1 to 3. As i said i won’t be going into further detail about regular expressions, but i will do that at a separate post in the future, i promise .
Searching Multiple Files && Printing Their Filenames
To finish this essay, i will describe what is probably the most important usage of grep and that is finding where some text is located through multiple files.
oddy-2:Desktop thought$ grep -lr line1 . ./new.txt
Using the -r switch, we do a recursive search of files, which means that grep will search each file from the current folder and all its subfolders to find the text you specify. Using the switch -l we ask from grep to print out the filenames for every successful match. Using -L, grep will print out the non matching filenames. We also specify the . in the end of the command to inform grep to start searching from the current directory.
I hope this tutorial will help you use grep more effectively in the future. If you want to ask something about it, please leave a comment. Thanx for reading !