Grep is one of the most common commands which we use in the terminal. Having used it quite a bit in my day to day work, I have decided to try to go in depth to list out some interesting applications

One of the best things about most command line utilities is using the man command to figure out what all can be done with a particular tool

From the man page this is the basic syntax of the command

SYNOPSIS
grep [OPTION...] PATTERNS [FILE...]
grep [OPTION...] -e PATTERNS ... [FILE...]
grep [OPTION...] -f PATTERN_FILE ... [FILE...]

How it works in a nutshell

the grep command goes through the provided files [FILE…] line by line and checks if the pattern provided is present in any line. If it is present then that line is printed as output to the command.

Examples

hello.txt

Hello, it's me
I was wondering if after all these years you'd like to meet
To go over everything
They say that time's supposed to heal ya
But I ain't done much healing
Hello, can you hear me?
I'm in California dreaming about who we used to be
When we were younger and free
I've forgotten how it felt before the world fell at our feet
There's such a difference between us
And a million miles
Hello from the other side
I must've called a thousand times
To tell you I'm sorry
For everything that I've done
But when I call you never
Seem to be home

To print all the lines which have the word Hello

grep "Hello" a.txt

which would give the output

Hello, it's me
Hello, can you hear me?
Hello from the other side

Printing lines with inverse match

if we output lines which don’t have the hello word, we need to use the -v or — invert-match flag

grep -v "Hello" a.txt

output

I was wondering if after all these years you'd like to meet
To go over everything
They say that time's supposed to heal ya
But I ain't done much healing
I'm in California dreaming about who we used to be
When we were younger and free
I've forgotten how it felt before the world fell at our feet
There's such a difference between us
And a million miles
I must've called a thousand times
To tell you I'm sorry
For everything that I've done
But when I call you never
Seem to be home

Print number of occurences of pattern

if instead of printing the lines we just want the number of lines which have a match we can use the -c flag.

grep -c "Hello" a.txt

output

3

Print filenames where the pattern occured

if we just want the filenames where the pattern occurred then we would just use the -l flag

b.txt

Something else

command

grep -l "Hello" a.txt b.txt

output

a.txt

Print contextual info around match

Sometimes we also want to print the lines around the matched line. Example if the match is on line 25 we also want to print line 24 and 26. For this we can make use of the -A, -B and -C flags

Command

grep -A 1 "Hello" a.txt

prints the matching string along with one line after it

Hello, it's me
I was wondering if after all these years you'd like to meet
--
Hello, can you hear me?
I'm in California dreaming about who we used to be
--
Hello from the other side
I must've called a thousand times

Similarly

  • -B n prints n lines before the matched line along with it
  • -C n prints n lines before and after the matched lines

Recursive Grep

Suppose we want to find all the filenames in a large project which have a particular pattern, then we can make use of recursive grep by using the -R flag

Example

grep -R -l "public" . 

would search for all files containing the `public` pattern in the current directory

Common applications

Grep is pretty powerful by itself but it really shines when used along with other utilities using the pipe operator