Showing posts with label Linux Grep Command. Show all posts
Showing posts with label Linux Grep Command. Show all posts

Thursday, 26 October 2017

Rhel 7 Grep Command

 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 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' /path/to/file

Use grep to search 2 different words

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

        or

Display cpu model name:

$ 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 command Description

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='--color=auto' GREP_COLOR='100;8

 

It will set the color for Matched Line and pattern found will be color

 

What is iSCSI and How Does it Work?