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

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


No comments:

Post a Comment