Tuesday, 6 August 2019

Linux - grep Command in Linux with Examples

grep Command

Grep command stands for Global Regular Expression Print. It is used to find specific contents in files. Grep is used to search the files based on patterns.

Syntax

grep < options> < patterns> < file-name>

Search pattern in one file

The following command is used to search a word in a single file.
Syntax
grep [word] [file name]
Example
[user1@linuxhelp Desktop]$ grep good test.txt 
Good morning

Search pattern in multiple files

The following command is used to search a word in multiple files.
Syntax
grep [word] [file 1] [file 2]
Example
[user1@linuxhelp Desktop]$ grep good test.txt test1.txt 
test.txt:good morning
test1.txt:good afternoon

Ignore Case while searching

The following command is used to ignore case while searching for a word in a file
Syntax
grep -i [word] [file name]
Example
[user1@linuxhelp Desktop]$ grep -i good test.txt 
good morning
Have a Good day

Find lines with starting string using regular expression

The following command is used to find lines with starting string or word using regular expression ‘ ^’ .
Syntax
egrep [^starting string ] [file name]
Example
[user1@linuxhelp Desktop]$ egrep  ^g  test.txt 
good morning

Find lines with ending string using regular expression

The following command is used to find lines with ending string or word using regular expression ‘ $’ .
Syntax
egrep [string$] [filename]
Example
[user1@linuxhelp Desktop]$ egrep y$ test.txt 
happy birthday

Search the lines using specified strings

The following command is used to search the lines using the specified strings.
Syntax
grep [ strings ] [file name]
Example
[user1@linuxhelp Desktop]$ grep [sinkri] test.txt 
good morning
happy birthday

Search the lines with digits

The following command is used to search lines with numbers.
Syntax
grep [0-9] [file name]
Example
[user1@linuxhelp Desktop]$ grep ' [0-9]'  test.txt 
2 1 4 9 8
1 6 1 2 9 3

Display lines after match

The following command is used to display lines after match in a file.
Syntax
grep -A [no. of lines] [file name]
Example
[user1@linuxhelp Desktop]$ grep -A 3 " good"  test.txt 
good morning
Have a Good day 
happy birthday

Display lines before match

The following command is used to display lines before match in a file
Syntax
grep -B [no. of lines] [file name]
Example
[user1@linuxhelp Desktop]$ grep  -A  3  " 2"   test.txt 
happy birthday
2 1 4 9 8
1 6 1 2 9 3

Display lines before and after match

The following command is used to display lines before and after match in a file.
Syntax
grep -C [no. of lines] [file name]
Example
[user1@linuxhelp Desktop]$ grep  -C  3  " happy"   test.txt 
good morning
Have a Good day 
happy birthday
2 1 4 9 8
1 6 1 2 9 3

Search in all files recursively

The following command is used to search word in all files in current directory and its sub directories.
Syntax
grep -r “ [word]” *
Example
[user1@linuxhelp Desktop]$ grep  -r  " good"   * 
test1.txt:good afternoon
find_pattern:^good
test.txt:good morning
test.txt~:good morning

Display number of matches count

The following command is used to display the number of matches count in a file.
Syntax
grep -c “ [word]” [filename]
Example
[user1@linuxhelp Desktop]$ grep -c " good"  test.txt 
1


Display only the matched word

The following command is used to display only the matched word from a file.
Syntax
grep -o “ [word]” [file name]
Example
[user1@linuxhelp Desktop]$ grep -o " day"  test.txt 
day

Display the output with line number

The following grep command is used to display the output with line number.
Syntax
grep -n “ [word]” [file name]
Example
[user1@linuxhelp Desktop]$ grep -n " day"  test.txt 
2:Have a Good day 
4:happy birthday

Display the files that matches the word

The following grep command is used to display the files that matches the word.
Syntax
grep -l [word] [files pattern*]
Example
[user1@linuxhelp Desktop]$ grep -l good test* 
test1.txt
test.txt
test.txt~

Reverse the matching pattern

The following grep command is used to reverse the matching pattern lines and display the other contents of the file.
Syntax
grep -v [word] [file name]
Example
[user1@linuxhelp Desktop]$ grep  -v  good  test.txt 
Have a Good day 
happy birthday
2 1 4 9 8
1 6 1 2 9 3

Using Grep with PS command

The following grep command is used to view the particular process use grep with ps command.
Syntax
ps aux | grep [process name]
Example
[user1@linuxhelp Desktop]$ ps aux | grep init 
root         1  0.1  0.1  19352  1536 ?        Ss   11:00   0:01 /sbin/init
user1     3559  0.0  0.0 103248   840 pts/0    S+   11:14   0:00 grep init

Search lines with specific words in a particular file

The following grep command is used to search lines with specific word in a particular file.
Syntax
egrep ' [word1]|[word2]|&hellip |&hellip ] [file name]'
Example
[user1@linuxhelp Desktop]$ egrep  ' good|day|2'   test.txt 
good morning
Have a Good day 
happy birthday
2 1 4 9 8
1 6 1 2 9 3

Search lines with specific words in all files in current working directory

The following grep command is used to search lines with specific word in all files in current working directory.
Syntax
egrep ' [word1]|[word2]|&hellip |&hellip ]' *
Example
[user1@linuxhelp Desktop]$ egrep  ' good|day|2'   * 
test1.txt:good afternoon
test.txt:good morning
test.txt:Have a Good day 
test.txt:happy birthday
test.txt:2 1 4 9 8
test.txt:1 6 1 2 9 3
test.txt~:good morning
test.txt~:Have a Good day 
test.txt~:happy birthday
test.txt~:2 1 4 9 8

Using Grep with find command

The following grep command is used to find files with the matching word in the current working directory.
Syntax
find . -type f -exec grep -l ‘ [word]’ {}
Example
[user1@linuxhelp Desktop]$ find . -type f -exec grep -l ' good'  {}  
./find_pattern
./test.txt
./test1.txt
./test.txt~

Display the line number of the matched string

The following grep command is used to display the line number of the matched string.
Syntax
grep -o -b “ [string]” [file name]
Example
[user1@linuxhelp Desktop]$ grep  -o  -b  " n"   test.txt 
8:n
10:n

Display the files that do not match the string

The following grep command is used to display the files in the current working directory that do not match the string.
Syntax
grep -L [string] *
Example
[user1@linuxhelp Desktop]$ grep  -L  " s"   * 
find_pattern
test1.txt
test.txt
test.txt~

Search multiple words in a file at the same time

The following grep command is used to search multiple words in a file at the same time.
Syntax
grep -e “ [word1]” -e “ [word2]” [file name]
Example
[user1@linuxhelp Desktop]$ grep -e " good"  -e " day"  test.txt 
good morning
Have a Good day 
happy birthday

Display the Grep output in color

The following grep command is used to display the Grep output in color
Syntax
grep -e “ [word1]” -e “ [word2]” [file name] --color
Example
[user1@linuxhelp Desktop]$ grep -e " good"  -e " day"  test.txt --color 
good morning
Have a Good day 
happy birthday

Search for the matching pattern using file

The following grep command is used to search the matching pattern using file.
Syntax
grep -f [file with patterns] [filename]
Example
[user1@linuxhelp Desktop]$ grep  -f  find_pattern  test.txt 
good morning
happy birthday

List the last executed Grep command with its output

The following grep command is used to list the last executed Grep command with its output.
Syntax
!grep
Example
[user1@linuxhelp Desktop]$ !grep 
grep -f find_pattern test.txt
good morning
happy birthday
How to display the count of a number of matches is exist which given by the word in "grep" command?
A
You can use the option of "-c" with "grep" command to display the count of a number of matches is exist which given by the word in "grep" command. For Ex: "grep -c "unix" filename.txt"
Q
How to show the line number while displaying the output using grep command in Linux?
A
Use the option of "-n" with "grep" command to show the line number while displaying the output for any file. For Syntax: "grep -n "unix" filename".
Q
How to fetch any word will ignore the case sensitive in any file using the "grep" command?
A
You can use the option of "-i" with "grep" command fetch any word will ignore the case sensitive in any file.
Q
How to use Linux "pipe" with "grep" command in Linux?
A
You can refer the following syntax to get idea about using Linux "pipe" with "grep" command in Linux. For Ex: cat /etc/passwd | grep 'root'.
Q
How Do I check the version of "grep" command in Linux?
A
You can use the option of "--help" (or) "man" page of "grep" command to check the version has been installed on the system. Syntax: "grep --help" (or) "man grep".

0 comments:

Post a Comment