0% found this document useful (0 votes)
4 views2 pages

RHEL 7 Grep Command

The document provides an overview of the grep command in Unix and Linux, detailing its function as a utility for searching plain-text data using regular expressions. It includes various examples of grep commands with different options, such as case-insensitive search, recursive search, counting matches, and displaying lines before and after matches. Additionally, it explains how to customize the output with color coding for matched lines.

Uploaded by

abhisheksafeeka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

RHEL 7 Grep Command

The document provides an overview of the grep command in Unix and Linux, detailing its function as a utility for searching plain-text data using regular expressions. It includes various examples of grep commands with different options, such as case-insensitive search, recursive search, counting matches, and displaying lines before and after matches. Additionally, it explains how to customize the output with color coding for matched lines.

Uploaded by

abhisheksafeeka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

GREP Command in Unix and Linux Examples

About Grep :

grep is a command-line utility for searching plain-text data sets for lines that match a regular expression

Grep Linux Command Grep Linux Command Description


Grep command Description
grep book /etc/passwd command to search book in a file

grep -i "book" /etc/passwd To ignore word case i.e match book, Book, BOOK and all
other combination with the -i option:

grep -R "192.168.1.5" /etc/ You can search recursively i.e. read all files under each
directory for a string “192.168.1.5”
grep -w "book" file The grep command to select only those lines containing
matches that form whole words i.e. match only book word:

egrep -w 'word1|word2' Use grep to search 2 different words


/path/to/file

grep -c 'word' /path/to/file The grep can report the number of times that the pattern
has been matched for each file using -c (count) option
grep -n 'root' /etc/passwd Pass the -n option to precede each line of output with the
number of the line in the text file from which it was obtained
grep -v mars /path/to/file You can use -v option to print inverts the match;
To print all line that do not contain the word mars:

dmesg | egrep '(s|h)d[a-z]' show the name of the hard disk devices:

grep -i 'Model' /proc/cpuinfo Display cpu model name:

or

$ grep --color vivek /etc/passwd force grep to display output in colors, enter:

$ grep -v -c this demo_file how many lines that does not match the pattern

Grep Command

grep -o -b "string" file.txt Displaying the position of the matched string in the line
The -b option allows the grep command to display the
character position of the matched string in a file.

grep -B 2 "Error" file.txt Displaying the lines before the match.


In log file it will tell lines around the error lines to know the
cause of the error.
This will prints the matched lines along with the two lines
before the matched lines.
grep -A 3 "Error" file.txt Displaying the lines after the match.
This will display the matched lines along with the three
lines after the matched lines.

grep -C 5 "Error" file.txt Displaying the lines around the match


This will display the matched lines and also five lines before
and after the matched lines

$ export GREP_OPTIONS='-- It will set the color for Matched Line and pattern found will
color=auto' be color
GREP_COLOR='100;8

You might also like