Linux GREP tutorial
GREP is a powerful tool to search for texts inside files with regular expressions.
To demonstrate what we're going to learn I have prepared a directory with the name of "quotes" containing 2 text files:
quotes
├── success.txt
└── programming.txt
quotes/success.txt
Success doesn’t just find you. You have to go out and get it.
Don't let yesterday take too much of today.
It's not whether you get knocked down, it's whether you get up.
Creativity is intelligence having fun.
80 percent of Success is showing up!
Today's accomplishments were yesterday's impossibilities.
You have a limited number of fucks to give in your life, use them wisely.
quotes/programming.txt
Code is like humor. When you have to explain it, it’s bad.
Simplicity is the soul of efficiency.
Before software can be reusable it first has to be usable.
Software testing is a sport like hunting, it's bughunting.
When Java is the only language you know, its limitations become serious.
Talk is cheap. Show me the code.
- You can recreate the directory on your system so you can practice as we go.
The tutorial has 6 parts:
- Introducing GREP
- Combining flags
- Searching inside directories
- Using regular expressions
- Searching for regex with grep -E
- Summary
1. Introducing GREP
We use GREP to search for texts.
The basic syntax is:
$ grep exp file
Were exp is the expression that we search for and file is where we search.
For example, to search for the string "success" inside "quotes.txt":
$ grep "success" quotes/success.txt
- Which returns nothing since no match was found.
On the other hand, let's search for "Success" with a capitalized first letter:
$ grep "Success" quotes/success.txt
- Which returns the result:
Success doesn’t just find you. You have to go out and get it.
80 percent of Success is showing up!
To make the command case insensitive use the -i flag:
$ grep -i "success" quotes/success.txt
Success doesn’t just find you. You have to go out and get it.
80 percent of Success is showing up!
Depending on your system, you may need to use the --color flag to highlight the matching phrase:
$ grep -i --color "success" quotes/success.txt
Success doesn’t just find you. You have to go out and get it.
80 percent of Success is showing up!
To see the line numbers use the -n option:
$ grep -n "Success" quotes/success.txt
2:Success doesn’t just find you. You have to go out and get it.
6:80 percent of Success is showing up!
2. Combining flags
We can use more than one flag at a time.
Here I combine -i for case insensitivity with -n to see the line numbers:
$ grep -in "success" quotes/success.txt
2:Success doesn’t just find you. You have to go out and get it.
6:80 percent of Success is showing up!
To see the lines before the match, use the -B flag while specifying the number of lines:
$ grep -in -B 2 "having fun" quotes/success.txt
3-Don't let yesterday take too much of today.
4-It's not whether you get knocked down, it's whether you get up.
5:Creativity is intelligence having fun.
Alternatively, to see the lines after the match, use the -A option while specifying the number of lines.
$ grep -in -A 3 "having fun" quotes/success.txt
5:Creativity is intelligence having fun.
6-80 percent of Success is showing up!
7-Today's accomplishments were yesterday's impossibilities.
8-You have a limited number of fucks to give in your life, use them wisely.
Or combine -A and -B to see a number of lines before and after the match:
$ grep -in -B 2 -A 2 "having fun" quotes/success.txt
3-Don't let yesterday take too much of today.
4-It's not whether you get knocked down, it's whether you get up.
5:Creativity is intelligence having fun.
6-80 percent of Success is showing up!
7-Today's accomplishments were yesterday's impossibilities.
3. Searching inside directories
You don't have to look specifically inside a single file, you can also search within a directory with the -r (recursive) flag:
$ grep -r "Simplicity" quotes/
quotes/programming.txt:Simplicity is the soul of efficiency.
To get the only the names of the files combine the flags -r and -l:
$ grep -rl "Simplicity" quotes/
quotes/programming.txt
To increase the chances of finding a match use the -i flag so you can find matching patterns no matter which case they are:
$ grep -ril "simplicity" quotes/
Use the -w flag to find matching words.
Let's find lines containing the word "up":
$ grep -w "up" quotes/success.txt
It's not whether you get knocked down, it's whether you get up.
80 percent of Success is showing up!
To limit the number of results, use the -m flag while specifying the maximum number of results.
For instance, looking for "humor" while limiting the results to not more than 2:
$ grep -m2 'humor' quotes/programming.txt
4. Using regular expressions with GREP
To find alternative expressions we use the pipe | symbol.
For example, searching for the expression "humor" or "wise":
$ grep -r 'humor\|wise' quotes/
quotes/success.txt:You have a limited number of fucks to give in your life, use them wisely.
quotes/programming.txt:Code is like humor. When you have to explain it, it’s bad.
- You need to escape the pipe for the expression to work \|.
You can use other special characters to find a matching pattern.
Use the character ^ (shift + 6) to search at the beginning of lines.
For example, let's search for "Talk" at the beginning of lines:
$ grep -ir "^talk" quotes/
quotes/programming.txt:Talk is cheap. Show me the code.
Use the $ sign to look at the end of line. Here I'm looking for lines that end with "up." (that's the word "up" followed by dot):
$ grep -inr "up\.$" quotes/
quotes/success.txt:4:It's not whether you get knocked down, it's whether you get up.
- In the expression "up\.$" I used the backslash to escape the dot since a dot has a meaning of any character.
Let's use the dot . operator to search for any line that ends with "up" followed by anything:
$ grep -inr "up.$" quotes/
quotes/success.txt:4:It's not whether you get knocked down, it's whether you get up.
quotes/success.txt:6:80 percent of Success is showing up!
5. Searching for regex with grep -E
The -E flag gives an extended version of regular expressions (regex).
Quantifiers in regular expressions are special characters whose job it is to indicate the amount of repetition in the expression they come after.
The simplest quantifier is braces which indicate the number of repetition. For example let's search for expressions with two p's in a row:
$ grep -E p{2} quotes/success.txt
A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
To specify a range we use {x, y} between x and y repetitions.
Also:
- {x,} denotes at least x repetitions.
- {,y} indicates not more than y repetitions.
Inside square brackets we put ranges of characters. For example:
- [a-z] is the range of all the letters from a to z.
- [A-Z] is the range of all the capital letters from A to Z
- [0-9] indicates all the digits from 0 to 9.
Let's search for any expression containing at least a single digit:
$ grep -E '[0-9]{1,}' quotes/success.txt
80 percent of Success is showing up!
The quantifier ? stands for zero or one repetitions.
So, the expression humou?r searches for both humor and humour:
$ grep -E humou?r quotes/programming.txt
Code is like humor. When you have to explain it, it’s bad.
6. Summary
Linux grep is a command-line tool for searching texts with regular expressions. In this tutorial, I barely scratched the surface of this powerful command. I highly recommand running man to be able to explore other interesting possibilities on your own:
$ man grep
Recommended for you:
Set NASA's picture of the day as the background of your PC with Bash