3

Click here to load reader

GE2155-grep

Embed Size (px)

Citation preview

Page 1: GE2155-grep

8/13/2019 GE2155-grep

http://slidepdf.com/reader/full/ge2155-grep 1/3

Unix Commands GE2155–Computer Practice II

http://cseannauniv.blogspot.in Vijai Anand 

Ex. No: 1.5 Regular Expression

AimTo search for regular expression in a file using grep command in unix.

A frequent requirement is to look for a pattern or expression in a file. Unix handles

this feature through  grep  and  egrep. grep  uses an regular expression to display lines thatmatch and  egrep enables searching for multiple patterns. Its usage is

gr ep   options searchtext filename

Command Functiongrep t hi s demo Lists the lines that contains the string thisgr ep ' end of ' demo Quotes mandatory for text containing spacegrep t hi s demo* Search this in multiple filesgrep –c t o demo  Number of occurrence of the word  to in the filegr ep –n sequence demo Display line numbers along with matching lines

gr ep –v wor d demo Displays lines that does not contain the text word gr ep –l vi m * Displays files containing text vimgr ep –i WORD demo Search for text ignoring case differencesgr ep ' [̂ 0- 9] ' demo Lines that start with a number gr ep ' [ 0- 9] $' demo Lines that end with a number l s - l | gr ep " d̂" Display the subdirectory namesgr ep –c " $̂" demo Display count of blank lines in the file.gr ep "2. . . . $" st ud Display lines that ends in the range 20000–29999egr ep "l ower | UPPER" demo Display lines that match either  lower or  upper egr ep "( pr evi ous| cur r ent )wor d" demo

Display lines that match either  previous word  or 

current word 

Result

Thus searching text patterns in files using grep has been completed successfully.

Page 2: GE2155-grep

8/13/2019 GE2155-grep

http://slidepdf.com/reader/full/ge2155-grep 2/3

Unix Commands GE2155–Computer Practice II

http://cseannauniv.blogspot.in Vijai Anand 

demo file

[ vi j ai @l ocal host r egexpr ] $ grep this demot hi s l i ne i s the 1st l ower case l i ne i n t hi s f i l e.

 Two l i nes above t hi s l i ne i s empt y.

[ vi j ai @l ocal host r egexpr ] $ grep 'end of' demo

1. e - go t o t he end of t he cur r ent wor d.

2. E - go t o t he end of t he curr ent WORD.

[ vi j ai @l ocal host r egexpr ] $ grep -c to demo

5

[ vi j ai @l ocal host r egexpr ] $ grep -n sequence demo15: WORD - WORD consi st s of a sequence of non- bl ank charact ers

16: Word - wor d consi st s of a sequence of l et t er s, di gi t s and underscor es.

[ vi j ai @l ocal host r egexpr ] $ grep -v word demo

 THI S LI NE I S THE 1ST UPPER CASE LI NE I N THI S FI LE.

t hi s l i ne i s the 1st l ower case l i ne i n t hi s f i l e.

 Thi s Li ne Has Al l I t s Fi r st Charact er Of The Wor d Wi t h Upper Case.

 Two l i nes above t hi s l i ne i s empt y.

vi m Word Navi gat i on

2. E - go t o t he end of t he curr ent WORD.

4. B - go to t he previ ous WORD.

WORD - WORD consi st s of a sequence of non- bl ank charact er s

t el net 172. 16. 4. 256

 THI S LI NE I S THE 1ST UPPER CASE LI NE I N THI S FI LE.t hi s l i ne i s the 1st l ower case l i ne i n t hi s f i l e. Thi s Li ne Has Al l I t s Fi r st Charact er Of The Wor d Wi t h Upper Case.

 Two l i nes above t hi s l i ne i s empt y.vi m Word Navi gat i on

 You may want t o do several navi gat i on i n r el at i on t o wor ds, such as:

1. e - go to t he end of t he cur r ent word.2. E - go t o t he end of t he cur r ent WORD.3. b - go t o t he pr evi ous word.4. B - go to t he previ ous WORD.

WORD - WORD consi st s of a sequence of non- bl ank char act ersWord - wor d consi st s of a sequence of l et t er s, di gi t s and underscor es.

t el net 172. 16. 4. 256

Page 3: GE2155-grep

8/13/2019 GE2155-grep

http://slidepdf.com/reader/full/ge2155-grep 3/3

Unix Commands GE2155–Computer Practice II

http://cseannauniv.blogspot.in Vijai Anand 

[ vi j ai @l ocal host r egexpr ] $ grep -l vim *

demo r eadme

[ vi j ai @l ocal host r egexpr ] $ grep -i WORD demo

 Thi s Li ne Has Al l I t s Fi r st Charact er Of The Wor d Wi t h Upper Case.

vi m Wor d Navi gati on

 You may want t o do sever al navi gat i on i n r el at i on t o wor ds, such as:1. e - go to the end of t he cur r ent word.

2. E - go t o the end of t he cur r ent WORD.

3. b - go t o the pr evi ous word.

4. B - go t o t he pr evi ous WORD.

WORD - WORD consi st s of a sequence of non- bl ank charact ers

Wor d - word consi st s of a sequence of l et t ers , di gi t s and under scores.

[ vi j ai @l ocal host r egexpr ] $ grep '^[0-9]' demo

1. e - go t o t he end of t he cur r ent wor d.

2. E - go t o t he end of t he curr ent WORD.

3. b - go t o the pr evi ous word.

4. B - go to t he previ ous WORD.

[ vi j ai @l ocal host r egexpr ] $ grep '[0-9]$' demo

t el net 172. 16. 4. 256

[ vi j ai @l ocal host vi j ai ] $ ls -l | grep "^d"

dr wxrwxr - x 2 vi j ai vi j ai 4096 Apr 9 14: 30 r egexpr

dr wxr wxr - x 7 vi j ai vi j ai 4096 Apr 4 14: 57 shel l scri pt s

[ vi j ai @l ocal host r egexpr ] $ grep -c "^$" demo

5

[ vi j ai @l ocal host r egexpr ] $ egrep "lower|UPPER" demo

 THI S LI NE I S THE 1ST UPPER CASE LI NE I N THI S FI LE.

t hi s l i ne i s the 1st l ower case l i ne i n t hi s f i l e.

[ vi j ai @l ocal host r egexpr ] $ egrep "(previous|current) word" demo

1. e - go t o t he end of t he cur r ent wor d.

3. b - go t o the pr evi ous word.