Skip to content
EuroVPSEuroVPS

How to use Grep

EuroVPS FAQ

Updated 5 min read

The ‘grep’ command is used to search a given file for patterns specified by the user which comes handy for file manipulation operations like an event search in logs files, configuration options in config files etc.

Strings must be enclosed in quotations to ensure the program correctly recognizes them as a single string if it includes a space or special character in it.

The grep command syntax is as noted below.

Usage: grep [OPTION]... PATTERN [FILE]..
Search for PATTERN in each FILE or standard input.

The sample file that we use here is named as eurovpstest.txt which include some test contents.

[root@eurovps ~]# cat eurovpstest.txt 
Managed VPS
Custom VPS
Bare Metal Servers
Private Cloud
Managed Clusters
cPanel Hosting
Agency Hosting
Since launching in April of 2004, EuroVPS has provided managed enterprise hosting in European datacenters.

Searching a string 

As with most Linux commands, grep searches are case sensitive by default

[root@eurovps ~]# grep hosting eurovpstest.txt 
Since launching in April of 2004, EuroVPS has provided managed enterprise hosting in European datacenters.

Case insensitive search

[root@eurovps ~]# grep -i  hosting eurovpstest.txt
cPanel Hosting
Agency Hosting
Since launching in April of 2004, EuroVPS has provided managed enterprise hosting in European datacenters.

Searching string with multiple words

[root@eurovps ~]# grep   "Custom VPS"  eurovpstest.txt  
Custom VPS

Strict word search

[root@eurovps ~]# grep -w VPS eurovpstest.txt
Managed VPS
Custom VPS

As you can see the grep search returned only specific strings which contain VPS and it excluded the strings like EuroVPS.

Searching multiple strings

[root@eurovps ~]# grep 'Servers\|Hosting' eurovpstest.txt
Bare Metal Servers
cPanel Hosting
Agency Hosting

We can include the case insensitive flag to get all the lines containing the word hosting irrespective of the alphabet case.

[root@eurovps ~]# grep -i 'Servers\|Hosting' eurovpstest.txt   
Bare Metal Servers
cPanel Hosting
Agency Hosting
Since launching in April of 2004, EuroVPS has provided managed enterprise hosting in European datacenters.

Grep to find number of occurrences of a string

[root@eurovps ~]# grep -c  hosting eurovpstest.txt 
1
[root@eurovps ~]# grep -ci hosting eurovpstest.txt
3

Output nearby entries above and below the match.

[root@eurovps ~]# grep --context=2  Private eurovpstest.txt 
Custom VPS
Bare Metal Servers
Private Cloud
Managed Clusters
cPanel Hosting

The abbreviated form of context flag is -C which can be used to get the same output string.

[root@eurovps ~]# grep -C 2  Private eurovpstest.txt 
Custom VPS
Bare Metal Servers
Private Cloud
Managed Clusters
cPanel Hosting

Similarly -A, --after-context=NUM and -B, --before-context=NUM can be used to get number of lines before and after the string match.

[root@eurovps ~]# grep -A 2  Private eurovpstest.txt 
Private Cloud
Managed Clusters
cPanel Hosting
[root@eurovps ~]# grep -B 2 Private eurovpstest.txt
Custom VPS
Bare Metal Servers
Private Cloud

Full flag list: grep --help or man grep.

From the EuroVPS FAQ.

More in how to

Your next infrastructure partner

Still have
a question?

Tell us what you are trying to do. An engineer will read it and reply.

+357 222 32280
A EuroVPS engineer carrying a server node on the datacenter floor
A conversation with the people
who will look after your infrastructure.

Let’s start with your setup.

A little context helps us start the right conversation.

We’ll use your details to respond to your enquiry. Privacy policy