Showing posts with label grep. Show all posts
Showing posts with label grep. Show all posts

>> egrep


egrep

egrep is the extended version of grep : Extended Global Regular Expressions Print.

Syntax:
egrep text/char/letter  fileName

Let's take a file file1 with below contents:
Summer is Beautiful.
A person should have beautiful mind.
Something is better than nothing.
We have 12 months in a year.
hello world !
New year occurs in January.
He fulfilled his wish.

1. egrep ll file1 ENTER  --- search lines having "ll" from the file.

Output:
hello world !
He fulfilled his wish.

OR
grep -E ll file1 ENTER

Output:
hello world !
He fulfilled his wish.

2. egrep [abc] file1   ---  returns lines containing either a or b or c

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

3. egrep [ABC] file1  --- lines containing letter A or B or C

egrep is case sensitive , above command returns below output:

Output:
Summer is Beautiful.
A person should have beautiful mind.

4. egrep [A-H] file1  --- lines containing the letters  A or B or C or D or E or F or H

Output:
Summer is Beautiful.
A person should have beautiful mind.
He fulfilled his wish.

Let's have another file file2 as below:
January is the 1st month
February is the second month.
A week has 7 days.
There are four seasons.
12 months in a year.
He works 24/7
We have 2 eyes to see.
We have two hands for hard work.

5. egrep [1-4] file2   ---  lines having the number 1 or 2 or 3 or 4

Output:
January is the 1st month
12 months in a year.
He works 24/7
We have 2 eyes to see.

6. egrep 'week|month|year' file2  ---  lines having the words " week " or "month"  or   "year"

Output:
January is the 1st month
February is the second month.
A week has 7 days.
12 months in a year.

7. egrep '^January|^A|^T' file2  --- lines starting with the word "January" or letter "A" or letter "T"

Output:
January is the 1st month
A week has 7 days.
There are four seasons.

Let's take a new file file3
January is the 1st month
February is the 2nd month.
1 week of rest
this is season 1
12 months in a year.
He works 24/7
We established in 1999
be Honest
5 boys are playing outside

8. egrep '^(1|F)' file3  --- lines starting with number "1" or letter "F"

Output:
February is the 2nd month.
1 week of rest
12 months in a year.


9. egrep '[1-9]$' file3  --- lines ending with number 1 to 9.

Output:
this is season 1
He works 24/7
We established in 1999

10. egrep '[a-zA-Z]$' file3   --- lines ending with alphabets lower case or upper case.

Output:
January is the 1st month
1 week of rest
be Honest
5 boys are playing outside

11. egrep '^1|t$' file3   --- lines which start with 1 or ends with "t" .

Output:
1 week of rest
12 months in a year.
be Honest

12. egrep -c 'month|week' file3  --- count number of lines meeting the condition

Output:
4

13. egrep -i 'january|honesT' file3  --- use "i" to ignore the case sensitivity.

Output:
January is the 1st month
be Honest

To search from more than one file

Let's take a file file1 with below contents:
Summer is Beautiful.
A person should have beautiful mind.
Something is better than nothing.
We have 12 months in a year.
hello world !
New year occurs in January.
He fulfilled his wish.

Let's have another file file2 as below:
January is the 1st month
February is the second month.
A week has 7 days.
There are four seasons.
12 months in a year.
He works 24/7
We have 2 eyes to see.
We have two hands for hard work.

Let's take a new file file3
January is the 1st month
February is the 2nd month.
1 week of rest
this is season 1
12 months in a year.
He works 24/7
We established in 1999
be Honest
5 boys are playing outside


14. egrep month file1 file2

Output:
file1:We have 12 months in a year.
file2:January is the 1st month
file2:February is the second month.
file2:12 months in a year.

15. egrep month file*

Output:
egrep month file*
file1:We have 12 months in a year.
file2:January is the 1st month
file2:February is the second month.
file2:12 months in a year.
file3:January is the 1st month
file3:February is the 2nd month.
file3:12 months in a year.

Print output with line number as well

16. egrep -n month file*

Output:
file1:4:We have 12 months in a year.
file2:1:January is the 1st month
file2:2:February is the second month.
file2:5:12 months in a year.
file3:1:January is the 1st month
file3:2:February is the 2nd month.
file3:5:12 months in a year.

17. egrep -n 'week|month' file1 file2

Output:
file1:4:We have 12 months in a year.
file2:1:January is the 1st month
file2:2:February is the second month.
file2:3:A week has 7 days.
file2:5:12 months in a year.

18. egrep -v month file1  --- returns output with out the line having the given word.

Output:
Summer is Beautiful.
A person should have beautiful mind.
Something is better than nothing.
hello world !
New year occurs in January.
He fulfilled his wish.

19. egrep -v 'month|week|year' file1 file2

Output:
file1:Summer is Beautiful.
file1:A person should have beautiful mind.
file1:Something is better than nothing.
file1:hello world !
file1:He fulfilled his wish.
file2:There are four seasons.
file2:He works 24/7
file2:We have 2 eyes to see.
file2:We have two hands for hard work.

20. egrep -nv ^S file1  --- returns lines with line numbers not starting with "S "

2:A person should have beautiful mind.
4:We have 12 months in a year.
5:hello world !
6:New year occurs in January.
7:He fulfilled his wish.


>> 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: