Day 7 of 90Days of Devops

I am a DevOps Engineer with an aim to learn and contribute to the Tech World!!
📝 Blogging and Sharing: On this platform, I'll be sharing insightful articles, tutorials, and tips on all things DevOps. Expect deep dives into CI/CD best practices, IaC patterns, containerization strategies, cloud optimization, and much more. Let's learn and grow together!
Grep Commands Brief Introduction
Grep stands for global regular expression print
The basic use of the grep command is to find a particular keyword in a file and also it can be used to find multiple keywords from multiple locations
Grep command syntax
grep [option] keyword filename
Here the keyword is the word that you are searching for
filename is the name of the file in which you are searching for the keyword
There are multiple options like -c, -i, -v, -w, -n use case of each we have explained below
Use Cases for grep command
- case 1) To ignore the upper and lower case while searching for the keyword
$ grep -i keyword filename
- case 2)To search for everything except the keyword that we are passing
$ grep -v keyword filename
- case 3) To print how many times a given keyword is present in a file
$ grep -c keyword filename
- case 4) To search for the exact match of a given keyword in a file
$ grep -w keyword filename
- case 5 )To print the line number for the matched keyword in the file
$ grep -n keyword filename
#Here output will be the line numbers in which the keyword is present
- case 6) To search for the keyword in multiple files
$ grep keyword file1 file2 file3
#Here file1 ,file2 and file3 are the files in which we are searcching for
#keywords
- case 7) To search for multiple keywords in a file
$ grep -e keyword1 -e keyword2 filename
#Here keyword1 is the first keyword that we are searching for
#keyword2 is the second keyword that we are searching for in file
#so in this way we can pass multiple keyword to search in a file
- case 8) To search for multiple keywords in multiple files
$ grep -e keyword1 -e keyword2 file1 file2 file3
#Here the above command will search for multiple keyword in multiple files
#like file1, file2, file3
- case 9)To search for keywords that we are passing in a file
$ grep -f kewords.txt file1 file2 file3
# Here we are passing a file named as keywords.txt this file contains all the
# keywords that we want to search for
#And this use case is helpful when we want to search for huge number of keyword
# file1 ,file2, file3 is the location where the keywords will be searched for
- case 10) Suppose we have 100 files in a directory (dirA) and we need to search for a keyword in all files
$ grep -R keyword dirA/
#Here -R means recursive so to check all files inside the directory recusively
- case 11) To print the matching lines which start with the given keyword
$ grep "^keyword" filename
example)
$ grep "^103" file1
#Here it will search for all the keywords that start with 103 in file1
- case 12) To print the matching lines which ends with the given keyword
$ grep "keyword$" filename
example)
$ grep "doctor$" file2
# Here it will serach for all the keywords that end with doctor in file2
⭐⭐😊😊Happy Learning to All !!⭐⭐😊😊
see you all in my next post !!!
Regards,
Amrit Manash

