Pages

Sunday, September 2, 2012

Grep Regular Expression


There are many times where a user need to search for some files, or for strings available in the files. Linux provides a regular expression tool called ‘GREP’

GREP searches named input files, or the standard input, and displays lines that match .One or more patterns called regular expressions or regexes can be used to obtain more better results. GREP can also search binary files and display records or buffers that contain matches.

Regular Expressions is nothing but a pattern to match for each input line. A pattern is a sequence of characters. Following all are examples of pattern:

Basic grep Regular Expression

Grep can be used with any command that generates the output. Consider the ‘ls’ command combined with grep,

[root@vx111a test]# ls /etc | grep syscon
drwxr-xr-x 11 root root            4096 Aug  3 17:15 sysconfig

I Used the ls command to generate a listing of all files available and the out put is sent to the grep command for finding a specific file (syscon in this command)

Now some more examples,
Search For Root in /etc/passwd :
[root@vx111a ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

Search For Multiple Strings in a file :
[root@vx111a test]# grep -E 'NTR|naren' test
NTR
naren

OR

Search For Multiple Strings in a File:
[root@vx111a perl]# grep 'mahesh\|NTR' samp
mahesh
NTR
Note : you must use the backslash \ to escape the OR operator (|).

Search Only Words
Some times when you search for a word ‘sam’ , the grep searches all the matches including samsam,ramsam . In order to make grep search for only words ,we can use

grep -w ‘sam” test

Count the Words
[root@vx111a test]# grep -c NTR test
1
[root@vx111a test]# grep -n NTR test
2:NTR ( show the line number too)

List File Names
[root@vx111a test]# grep -i NTR *
test:NTR (shows the File Name )

Results In Color
[root@vx111a test]# grep --color=auto NTR *
test:NTR
One More ,
[root@vx111a test]# grep -E --color=auto 'NTR|Manhesh' test
Manhesh
NTR

Remove Grep While Grepping Some thing
consider , when you try to grep some thing from the ps command like this ,it also shows
[root@vx111a test]# ps ux | grep gnome-terminal
root      3349  0.1  0.4  42280 13632 ?            Ssl  13:31   0:01 gnome-terminal
root      3740  0.0  0.0   4016   688 pts/1         S+   13:54   0:00 grep gnome-terminal
Both the search result as well as the grep command is also shown in the results. In order remove the grep ,we can use
[root@vx111a test]# ps ux | grep -v grep | grep gnome-terminal
root      3349  0.1  0.4  42280 13632 ?            Ssl  13:31   0:01 gnome-terminal

Or

[root@vx111a test]# ps ux |  grep [g]nome-terminal
root      3349  0.1  0.4  42280 13632 ?            Ssl  13:31   0:01 gnome-terminal

Anchors
There is a facility provided by Grep to search start and end positions. We can use anchors which help in forcing a regex to match only at start or end of lines respectively, like

Search For all Lines that start with ‘s’ :
[root@vx111a ~]# grep ^s /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
squid:x:23:23::/var/spool/squid:/sbin/nologin
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin

Search for all string that end with ‘bash’
[root@vx111a ~]# grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

Match line only containing foo:
grep '^foo$' filename

You can search for blank lines with the following examples:
grep '^$' filename

Character Class
Using Character Class, we can tell the grep to match only one out of the several characters. This can be done simply by placing the characters you want to match between the square brackets.

Search for characters ‘v’ or ‘V’ in the file like
[root@vx111a test]# grep '[vV]' test
Chiranjeevi
Pavan
Bhuvan
Vivek
vivek
mehtavivek
VivekRao
v0091mehta

To Find Words which starts either with ‘v’ or ‘V’
[root@vx111a test]# grep '[vV]ivek' test
Vivek
vivek
mehtavivek
VivekRao
Some More ,

Some More Examples
[root@vx111a test]# grep 'v[0]' test
v0091mehta

[root@vx111a test]# grep '[vk'] test
Chiranjeevi
Pavan
Bhuvan
Vivek
vivek
mehtavivek
VivekRao
v0091mehta

Within a bracket expression, the name of a character class enclosed in "[:" and ":]" stands for the list of all characters belonging to that class. Standard character class names are:
* [:alnum:] - Alphanumeric characters.
* [:alpha:] - Alphabetic characters
* [:blank:] - Blank characters: space and tab.
* [:digit:] - Digits: '0 1 2 3 4 5 6 7 8 9'.
* [:lower:] - Lower-case letters: 'a b c d e f g h i j k l m n o p q r s t u v w x y z'.
* [:space:] - Space characters: tab, newline, vertical tab, form feed, carriage return, and space.
* [:upper:] - Upper-case letters: 'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'.

In this example match all upper case letters:
grep '[:upper:]' filename

[root@vx111a perl]# grep [:UPPER:] test
JAGADESH
KIRAN

[root@vx111a perl]# grep '[:alpha:]' test
mehtaVivek
Vevikmehta
Vardham
v0091mehta
pavan
2134lkdsafj
dsankld

Wild Cards
Grep also provides support for wild card search. Consider the file ‘sam’ with the contents,
[root@vx111a test]# cat sam
big
bad bug
bag
bigger
boogy

Now if we need to search for words starting with b and end with g , we can use
[root@vx111a test]# grep b.g sam
big
bad bug
bag
bigger
and also
[root@vx111a test]# grep '\' sam
big
bad bug
bag

Display lines starting with (.) dot
grep ‘^\.[0-9]’ sam

More Advanced Grep options
[root@vx111a test]# cat sample
100 sai             Manager           Sales                $5,000
200 kai             Developer        Technology      $5,000
300 rai             Admin              Technology      $7,000
400 jai              Tester              Market              $9,000
500 mai           Developer        Sales                $6,000

Grep either Tech Or Sales
[root@vx111a test]# grep 'Tech\|Sales' sample
100 sai             Manager           Sales                $5,000
200 kai             Developer        Technology      $5,000
300 rai             Admin              Technology      $7,000
500 mai           Developer        Sales                $6,000

Using Multiple -e
[root@vx111a test]# grep -e Tech -e Sales sample
100 sai             Manager           Sales                $5,000
200 kai             Developer        Technology      $5,000
300 rai             Admin              Technology      $7,000
500 mai           Developer        Sales                $6,000

Dev and Tech in One line
[root@vx111a test]# grep -E 'Dev.*Tech' sample
200 kai             Developer        Technology      $5,000

Grep Both Tech and Admin in one line
[root@vx111a test]# grep Tech sample | grep Admin
300 rai             Admin              Technology      $7,000

Happy Coding , More To Come