1
|
grep Fruit sample.txt
|
This will show all lines with pattern Fruit .
|
2
|
grep Fruit G*
|
This searches pattern Fruit in all files starting with G .
|
3
|
grep '^M' sample.txt
|
This searches all lines starting with M .
|
4
|
grep '6$' sample.txt
|
This searches lines ending with 6 .
|
5
|
grep '1..' sample.txt
|
This displays lines containing 1 and any character after it.
|
6
|
grep '.6' sample.txt
|
This shows lines containing .6 .
|
7
|
grep '^[AT]' sample.txt
|
This searches lines starting with A or T .
|
8
|
grep '[^0-9]' sample.txt
|
This contains at least one alphabet.
|
9
|
grep '[A-Z][A-Z] [A-Z]' sample.txt
|
This searches the upper case, upper case space, and upper case word.
|
10
|
grep '[a-z]{8}' sample.txt
|
This displays all lines in which there are at least eight consecutive lowercase letters.
|
11
|
grep '<Fruit' sample.txt
|
This displays all lines containing a word starting with Fruit. The < is the beginning-of-word anchor.
|
12
|
grep '<Fruit>' sample.txt
|
This displays the line if it contains the word Fruit .
The < is the beginning-of-word anchor and the > is the end-of-word anchor.
|
13
|
grep '<[A-Z].*o>' sample.txt
|
This displays all lines containing a word starting with an uppercase letter, followed by any number of characters and a word ending in o .
|
14
|
grep -n '^south' sample.txt
|
This displays line numbers also.
|
15
|
grep –i 'pat' sample.txt
|
This displays case insensitive search.
|
16
|
grep -v 'Onion' sample.txt > temp
mv temp sample.txt
|
This deletes the line containing pattern.
|
17
|
grep –l 'Nuts' *
|
This lists files containing pattern.
|
18
|
grep –c 'Nuts' sample.txt
|
This prints the number of lines where pattern is present.
|
19
|
grep –w 'Nuts' sample.txt
|
This counts where the whole world pattern is present, not a part of the word.
|