RHEL 7 Grep Command
RHEL 7 Grep Command
About Grep :
grep is a command-line utility for searching plain-text data sets for lines that match a regular expression
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:
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:
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.
$ export GREP_OPTIONS='-- It will set the color for Matched Line and pattern found will
color=auto' be color
GREP_COLOR='100;8