Home » Linux » Linux Commands » Use of GREP command with examples in Unix / Linux

Use of GREP command with examples in Unix / Linux

What is GREP ?

Unix / Linux provides a powerful tool to search the contents of one or more text files, or to search the contents of input provided, for matching a pattern. This is referred to as pattern matching (a.k.a. regular expression or globbing). A pattern can be a single character , a series of characters, a word, or a sentence. You must enclose the pattern in double quotes if it contains one or more white spaces.

The command used for pattern matching is called grep (global regular expression print), and it searches contents of one or more specified files for a regular expression. If the expression is found, grep prints every line containing that expression on the screen without changing the contents of the original file. use of grep command with examples in unix / linux.

Use of grep command with examples in Unix / Linux

To search for the pattern unixhops in the /etc/passwd file:

root@unixhops:~# grep unixhops /etc/passwd
unixhops:x:500:500::/home/unixhops:/bin/bash

To search for all occurrences of the pattern unixhops both the /etc/passwd and /etc/group files:

root@unixhops:~# grep root /etc/passwd /etc/group
/etc/passwd:unixhops:x:500:500::/home/unixhops:/bin/bash
/etc/group:unixhops:x:500:

pattern unixhops from the specified file list, use the -l option:

 [root@unixhops ~]# grep -l unixhops /etc/group /etc/passwd /etc/hosts
 /etc/group
 /etc/passwd

To search for all lines in the /etc/passwd file that begin with the pattern root . The bash shell treats the caret ^ sign as a special character which marks the beginning of a line or word. This is useful, for instance, if you wish to know whether there is more than one user with that name.

 [root@unixhops ~]# grep ^root /etc/passwd
 root:x:0:0:root:/root:/bin/bash

To list all lines from the /etc/passwd file that end with the pattern bash. The bash shell treats the dollar sign ($) as a special character which marks the end of a line or word. This is useful, for example, to determine which users have their shells set to the bash shell.

 [root@unixhops ~]# grep bash$ /etc/passwd
 root:x:0:0:root:/root:/bin/bash
 unixhops:x:500:500::/home/unixhops:/bin/bash

To search for all empty lines in the /etc/passwd file:

# grep ^ $ /etc/passwd

to search for all lines in the /etc/passwd file that contain only the pattern root:

# grep ^ root $ /etc/passwd

To search for all lines in the /etc/passwd file that contain the pattern root. The -i option used with grep here ignores the letter case. This is useful to determine if there are root user accounts with a combination of lowercase and uppercase letters.

 [root@unixhops ~]# grep -i root /etc/passwd
 root:x:0:0:root:/root:/bin/bash
 operator:x:11:0:operator:/root:/sbin/nologin

Use grep with pipe

to find some pattern in value in a certain output have to pass the output using pipe

example :

 [root@unixhops ~]# ifconfig | grep inet\ addr
 inet addr:10.0.11.253 Bcast:10.255.255.255 Mask:255.0.0.0
 inet addr:192.168.2.1 Bcast:192.168.1.255 Mask:255.255.255.0
 inet addr:127.0.0.1 Mask:255.0.0.0

Check Also

tar command examples

TAR command examples in Unix / Linux

The “tar” command in linux used for making archive of files and directories. Using “tar” …

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: