grep command in linux

The word grep stands for Globally Regular Expression Print. Using grep command in Linux/Unix, we can search one or more files for lines that contains pattern.

Syntax:

grep [options] pattern [files]

Options:

Option Description
-b Displays the block number at the beginning of each line.
-c Displays the number of matched lines
-h Displays the matched lines, but do not display the file names
-i To ignore Case Sensitivty
-l Displays the filename, but do not display the matched lines.
-n Displays the matched lines and their line numbers
-v Displays the all lines that do not match
-w Match whole word

Examples:

1. To search for a word root in /etc/passwd file

grep command in linux

2. To search block number of the result pattern, this gives list of the lines pattern found along with the block number of line.

grep command in linux1

3. To count number of lines the pattern
grep command in linux2

4. If we want only lines and no file names we can go with -h option.

grep command in linux3

5. If we want only file names and no lines the use -l option.

grep command in linux4

6. In some cases we may require the output of content excluding the lines that we find the pattern in such cases we have to go with -v option.

grep command in linux

Related Posts

Responses are currently closed, but you can trackback from your own site.

Comments are closed.

Powered by k2schools