03 GREP - kumar159man/MyShellLearning GitHub Wiki
grep is a command-line utility for searching plain-text data sets for lines that match a regular expression
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$
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep "bash" first.sh second.sh first.sh:#!/usr/bin/bash second.sh:#!/usr/bin/bash
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
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
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$
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
o flag is used for displaying the string
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -o "Four" grepUsage.txt Four myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$
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$
c flag is used for displaying the matched number of lines
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -cw "line" grepUsage.txt 7
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
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
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
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$
l flag will display the file name
myubuntu@myubuntu-VirtualBox:~/Desktop$ grep -rl "bash" * shellScripts/first.sh shellScripts/third.sh shellScripts/second.sh
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
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
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