http://www.technicalpage.net/search/label/SQL

>> grep


grep


grep is one of the most powerful command in UNIX.

grep is mainly used to search string/character/word  in a file. grep has other benefits also as discussed below.

search string/character/word  in a file or files:

Syntax:
grep {text/string}  {filename}

suppose file1 has below text:
This is a beautiful world.
Life is beautiful with family.
Rainbow is colorful.
The best and the most beautiful things in the world can not be seen or even touched.
Nature is incredible.

Now, we want to search the word "beautiful" in the file file1.

command:
grep 'beautiful'  file1

output:
This is a beautiful world.
Life is beautiful with family.
The best and the most beautiful things in the world can not be seen or even touched.

grep is case sensitive , if you give the command like , grep 'Beautiful'  file1 , it will search for the word "Beautiful" not the word "beautiful". So this command will return nothing from the file file1.

To remove the  case sensitivity, below command will return the matching character irrespective of the character case.

Syntax:
grep  -i  'character in Upper case or lower case or combination'  fileName

grep  -i  'BEAUTIFUL'  file1
This will return below result which is same like previous result.

Output:
This is a beautiful world.
Life is beautiful with family.
The best and the most beautiful things in the world can not be seen or even touched.

To know/return the lines without the given word:

Syntax:
grep  -v  'character/word'  fileName 

grep   -v  'beautiful'  file1

Output:
Rainbow is colorful.
Nature is incredible.

To display  the line number also with the output:

Syntax:
grep   -n   'character/word'  fileName

grep  -n  'beautiful'  file1

Output:
1:This is a beautiful world.
2:Life is beautiful with family.
4:The best and the most beautiful things in the world can not be seen or even touched.

To display the string with more than one word :

Syntax:
grep  'string with more than one words or sentence'  fileName

grep  ' most beautiful things'  file1

Output:
The best and the most beautiful things in the world can not be seen or even touched.

Search whole word only:

Normal grep will return where ever it finds the match whether whole word or part of a word.

grep  'or'  file1   --- This will search the 'or'  whether whole word or part of a word.

Output:
This is a beautiful world.
Rainbow is colorful.
The best and the most beautiful things in the world can not be seen or even touched.

To search whole word only:

grep  -w  'or'  file1   ---  This will search  the whole word  'or' only , not this being part of another word.

Output;
The best and the most beautiful things in the world can not be seen or even touched.


grep  can be used to find a particular word or string from more than one files.

Syntax:
grep  'word/character'  fileName1   fileName2   fileName3  ....

Suppose we have two files as below:
file1:
This is a beautiful world.
Life is beautiful with family.
The best and the most beautiful things in the world can not be seen or even touched.
Rainbow is colorful.
Nature is incredible.

file2:
Summer is Beautiful.
A person should have beautiful mind.
Something is better than nothing.
We have 12 months in a year.

Command:
grep   'beautiful'   file1   file2

Output:
file1:This is a beautiful world.
file1:Life is beautiful with family.
file1:The best and the most beautiful things in the world can not be seen or even touched.
file2:A person should have beautiful mind.

regular expression:

The above command can also be written as, using regular expression ;
grep  'beautiful'  file*
(if the files have extension, then file*.*)

Output:
file1:This is a beautiful world.
file1:Life is beautiful with family.
file1:The best and the most beautiful things in the world can not be seen or even touched.
file2:A person should have beautiful mind.


Using regular expression, we can search string with part of the text known:

Syntax:
grep  'starting_part*.*ending_part'  fileName

grep   'beaut*.*ld'   file1
Output:
This is a beautiful world.
The best and the most beautiful things in the world can not be seen or even touched.

if you know one word completely and  know  the second word partially only:

grep  'beautiful.*ld'   file1

Output:
This is a beautiful world.
The best and the most beautiful things in the world can not be seen or even touched.


"^"  in grep:
To search the line starting with given text/word :

Syntax:
grep  ^word  fileName

grep  ^The   file1

Output:
The best and the most beautiful things in the world can not be seen or even touched.

grep  ^Th   file1

Output:
This is a beautiful world.
The best and the most beautiful things in the world can not be seen or even touched.

"$"  in grep:
To know the line/s  which  end with the given word.

Syntax:
grep   word$   fileName

grep   family.$   file1

Output:
Life is beautiful with family.

grep  ful.$   file1

Output:
Rainbow is colorful.


To return the fixed length words:

Syntax:

grep  ^{number of dots for the given word/character size}$   fileName
 
Suppose we have a file "days":
Sunday
Monday
TuesDay
WednesDay

ThursDay

Friday
SaturDay
A weekend is Saturday and Sunday.
Thursday is a day between Wednesday and Friday.


To return the texts with 6 character length.

grep ^......$ days

Output:
Sunday
Monday
Friday

To retrun blank lines:

Syntax:
grep  ^$  {fileName}

grep   ^$   days
Output: --> returns two blank lines as the file "days" is having two blank lines


To make the output(blank lines) visible, lets display  the respective line number also .

grep   -n  ^$   days

Output:

5:

7:

No comments:

Post a Comment