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]
grep [word] [file name]
Example
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
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]
grep -i [word] [file name]
Example
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]
egrep [^starting string ] [file name]
Example
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]
egrep [string$] [filename]
Example
Search the lines using specified strings
The following command is used to search the lines using the specified strings.
Syntax
grep [ strings ] [file name]
grep [ strings ] [file name]
Example
Search the lines with digits
The following command is used to search lines with numbers.
Syntax
grep [0-9] [file name]
grep [0-9] [file name]
Example
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]
grep -A [no. of lines] [file name]
Example
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]
grep -B [no. of lines] [file name]
Example
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]
grep -C [no. of lines] [file name]
Example
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]” *
grep -r “ [word]” *
Example
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]
grep -c “ [word]” [filename]
Example
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]
grep -o “ [word]” [file name]
Example
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]
grep -n “ [word]” [file name]
Example
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*]
grep -l [word] [files pattern*]
Example
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]
grep -v [word] [file name]
Example
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]
ps aux | grep [process name]
Example
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]'
egrep ' [word1]|[word2]|&hellip |&hellip ] [file name]'
Example
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 ]' *
egrep ' [word1]|[word2]|&hellip |&hellip ]' *
Example
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]’ {}
find . -type f -exec grep -l ‘ [word]’ {}
Example
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]
grep -o -b “ [string]” [file name]
Example
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] *
grep -L [string] *
Example
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]
grep -e “ [word1]” -e “ [word2]” [file name]
Example
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
grep -e “ [word1]” -e “ [word2]” [file name] --color
Example
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]
grep -f [file with patterns] [filename]
Example
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
!grep
Example
0 comments:
Post a Comment