03 GREP - kumar159man/MyShellLearning GitHub Wiki

Grep

grep is a command-line utility for searching plain-text data sets for lines that match a regular expression

Usage of grep

Let's use a sample file

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat grepUsage.txt 
One line of lines
Two line of lines
Three line of lines
Four line of lines
Five line of lines
Six line of lines
Seven line of lines
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep "Three" grepUsage.txt 
Three line of lines

If the string is not available then there is no output

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep "Ten" grepUsage.txt 
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ 

Using grep in multiple files

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep "bash" first.sh second.sh
first.sh:#!/usr/bin/bash
second.sh:#!/usr/bin/bash

Using grep with output of a command

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ls
first.sh  grepUsage.txt  second.sh  third.sh  variables.txt
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ls | grep "first"
first.sh

grep with i flag

i is the flag for ignoring case

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat grepUsage.txt 
One line of lines
Two line of lines
Three line of lines
Four line of lines
Five line of lines
Six line of lines
Seven line of lines
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep "one" grepUsage.txt 
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ 

Let's use -i flag

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -i "one" grepUsage.txt 
One line of lines

Use of w flag with grep

w flag is used for matching a word

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep "line" grepUsage.txt 
One line of lines
Two line of lines
Three line of lines
Four line of lines
Five line of lines
Six line of lines
Seven line of lines
End of lines of lines

if w flag is not used then grep also filters lines

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -w "line" grepUsage.txt 
One line of lines
Two line of lines
Three line of lines
Four line of lines
Five line of lines
Six line of lines
Seven line of lines
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ 

Use of v flag with grep

v flag is used for displaying lines which doesn't consist the string

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -v "Four" grepUsage.txt 
One line of lines
Two line of lines
Three line of lines
Five line of lines
Six line of lines
Seven line of lines
End of lines of lines

Use of o flag with grep

o flag is used for displaying the string

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -o "Four" grepUsage.txt 
Four
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ 

Use of n flag with grep

n flag is used for displaying the line number

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -n "Four" grepUsage.txt 
4:Four line of lines
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$

Use of c flag with grep

c flag is used for displaying the matched number of lines

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -cw "line" grepUsage.txt 
7

Use of A flag with grep

A flag is used for displaying lines after the match

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -A3 "Three" grepUsage.txt 
Three line of lines
Four line of lines
Five line of lines
Six line of lines

Use of B flag with grep

B flag is used for displaying lines after the match

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -B2 "Three" grepUsage.txt 
One line of lines
Two line of lines
Three line of lines

Use of C flag with grep

C flag is used for displaying lines after and before the match

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -C2 "Three" grepUsage.txt 
One line of lines
Two line of lines
Three line of lines
Four line of lines
Five line of lines

Use of r flag with grep

r flag is for recursive

myubuntu@myubuntu-VirtualBox:~/Desktop$ ls
docKerFile  gitDockerCourse  shellScripts
myubuntu@myubuntu-VirtualBox:~/Desktop$ grep "bash" *
grep: docKerFile: Is a directory
grep: gitDockerCourse: Is a directory
grep: shellScripts: Is a directory
myubuntu@myubuntu-VirtualBox:~/Desktop$ grep -r "bash" *
shellScripts/first.sh:#!/usr/bin/bash
shellScripts/third.sh:#!/usr/bin/bash
shellScripts/second.sh:#!/usr/bin/bash
myubuntu@myubuntu-VirtualBox:~/Desktop$ 

Use of l flag with grep

l flag will display the file name

myubuntu@myubuntu-VirtualBox:~/Desktop$ grep -rl "bash" *
shellScripts/first.sh
shellScripts/third.sh
shellScripts/second.sh

Use of f flag

f flag is used to read the search string from a file Let's create a file search.txt

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat search.txt 
Three
Seven
Two

Now let's use -f flag

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -f search.txt grepUsage.txt
Two line of lines
Three line of lines
Seven line of lines

Use of e flag

e flag can be used to search multiple string

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -e "Three" -e "Seven" -e "Two" grepUsage.txt 
Two line of lines
Three line of lines
Seven line of lines

Use of E flag

E flag is used for patterns

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -oE "li(.*?)" grepUsage.txt 
line of lines
line of lines
line of lines
line of lines
line of lines
line of lines
line of lines
lines of lines
⚠️ **GitHub.com Fallback** ⚠️