-
grepstands for:- Globally search a Regular Expression and Print (common backronym)
- Global Regular Expression Print
- Global Regular Expression Parser
-
We can use the
grepcommand to search for a specified pattern within a single or multiple files.
grep <pattern> <filename>- This command prints all lines in the file(s) that match the specified pattern.
Assume we have a file subjects.txt with the following contents:
Sno Subjectname Faculty Fee
1. Python Nagoor 1000
2. Java Sriman 2000
3. Unix Durga 250
4. DevOps Sriman 3500
5. UNIX Durga 400
6. Java Durga 1000
To search for the word "durga" in subjects.txt, you can use any of these commands:
$ grep 'durga' subjects.txt
$ grep "durga" subjects.txt
$ grep durga subjects.txt- This will print all lines that contain "durga" (case-sensitive):
3. Unix Durga 250
5. UNIX Durga 400
6. Java Durga 1000
Consider the demo.txt
The most important subject is linux
learning linux is very easy
the knowledge of linux is required everywhere
Your father learn linux, now you are learning.
In the future your kids also required to learn linux
java is a programming language
python is all rounder
Devops is the best technologies to fulfill current market requirements
akshay@myubuntu:~/Desktop$ grep "linux" demo.txt
The most important subject is linux
learning linux is very easy
the knowledge of linux is required everywhere
Your father learn linux, now you are learning.
In the future your kids also required to learn linuxsubject.txt
Linux is very important everywhere
Java is like ocean and not that much easy
Python is nursery level programming language
Devops and datascience have future
Linux videos are too good
akshay@myubuntu:~/Desktop$ grep linux demo.txt subjects.txt
demo.txt:The most important subject is linux
demo.txt:learning linux is very easy
demo.txt:the knowledge of linux is required everywhere
demo.txt:Your father learn linux, now you are learning.
demo.txt:In the future your kids also required to learn linuxSearch in all file in current working directory
akshay@myubuntu:~/Desktop$ grep java *
demo.txt:java is a programming languageIt cannot search in nested directory:
akshay@myubuntu:~/Desktop$ grep linux *
demo.txt:The most important subject is linux
demo.txt:learning linux is very easy
demo.txt:the knowledge of linux is required everywhere
demo.txt:Your father learn linux, now you are learning.
demo.txt:In the future your kids also required to learn linux
grep: dir1: Is a directory
akshay@myubuntu:~/Desktop$ You can use grep to search for a pattern across multiple files. Here are a few examples:
$ grep durga subjects.txt career.txt- Output:
subjects.txt:3. unix durga 250
subjects.txt:5. UNIX durga 400
subjects.txt:6. Java durga 1000
career.txt:durga
career.txt:durga ksdjfdlakjklfjad
$ grep durga *.txt- Output:
career.txt:durga
career.txt:durga ksdjfdlakjklfjad
subjects.txt:3. unix durga 250
subjects.txt:5. UNIX durga 400
subjects.txt:6. Java durga 1000
$ grep durga *- Output:
career.txt:durga
career.txt:durga ksdjfdlakjklfjad
subjects.txt:3. unix durga 250
subjects.txt:5. UNIX durga 400
subjects.txt:6. Java durga 1000
By default, the grep command is case-sensitive. If you want to ignore case, you should use the -i option.
$ grep unix *.txt- Output:
career.txt:unix jksadjfklasjdkflajs
subjects.txt:3. unix durga 250
$ grep -i unix *.txt- Output:
career.txt:unix jksadjfklasjdkflajs
subjects.txt:3. unix durga 250
subjects.txt:5. UNIX durga 400
To count the number of occurrences of a pattern, use the -c option. The c stands for count.
$ grep -c unix *.txt- Output:
career.txt:1
subjects.txt:1
| Command | Purpose | Example Usage |
|---|---|---|
locate |
Quickly finds files and directories by name or pattern using an indexed database. | locate filename.txt |
find |
Searches for files and directories by name, type, size, and other attributes in real-time. | find /home -name filename.txt |
grep |
Searches for specific text patterns within the content of files. | grep "pattern" filename.txt |
locateandfind: These commands are primarily used to find the location of files and directories based on their names or other attributes.grep: This command is used to search for specific patterns or text within the contents of files.
In essence:
- Use
locateandfindto locate files. - Use
grepto search within the content of those files.
Use the -n option to display line numbers for each matched line in the search results.
$ grep -n is *.txt- Output:
career.txt:1:Java It is ocean but ever green!!!!
$ grep -n durga *.txt- Output:
career.txt:4:durga
career.txt:7:durga ksdjfdlakjklfjad
subjects.txt:4:3. unix durga 250
subjects.txt:6:5. UNIX durga 400
subjects.txt:7:6. Java durga 1000
Use the -l option to display only the names of files that contain the specified pattern.
$ grep -l durga *.txt- Output:
career.txt
subjects.txt
Use the -v option to invert the search, displaying lines that do not contain the specified pattern.
$ grep durga subjects.txt- Output (shows lines with "durga"):
3. unix durga 250
5. UNIX durga 400
6. Java durga 1000
$ grep -v durga subjects.txt- Output (shows lines without "durga"):
sno subjectname faculty fee
1. Python Nagoor 1000
2. java Sriman 2000
4. devops sriman 3500
Use the -w option to search for whole words that match the pattern exactly.
$ grep -i unix demo.txt- Output (case-insensitive search):
UnixDemo session
unix material
$ grep -iw unix demo.txt- Output (case-insensitive, exact word match):
unix material
The -in and -win options allow you to search for exact words, with -i making the search case-insensitive and -n displaying line numbers.
$ grep -in unix demo.txt- Output (case-insensitive search with line numbers):
5:UnixDemo session
6:unix material
9:UNIX classes and videos
$ grep -win unix demo.txt- Output (whole-word match, case-insensitive, with line numbers):
6:unix material
9:UNIX classes and videos
To display additional lines around each match, use the -A, -B, or -C options.
-A <number>: Shows the specified number of lines after each match.-B <number>: Shows the specified number of lines before each match.-C <number>: Shows the specified number of lines both before and after each match.-<number>: TheCis optional for showing lines before and after.
$ grep friends demo.txt- Output (shows only the matched line):
Hello friends how are you
$ grep -A 2 friends demo.txt- Output (shows 2 lines after the matched line):
Hello friends how are you
DataScienceDemo
UnixDemo session
$ grep -B 2 friends demo.txt- Output (shows 2 lines before the matched line):
This is java demo
this is python demo
Hello friends how are you
$ grep -C 2 friends demo.txt- Output (shows 2 lines before and after the matched line):
This is java demo
this is python demo
Hello friends how are you
DataScienceDemo
UnixDemo session
$ grep -2 friends demo.txt- Output (shows 2 lines before and after the matched line,
Cis optional):
This is java demo
this is python demo
Hello friends how are you
DataScienceDemo
UnixDemo session