grep command in Linux/Unix
grep (global regular expression parse) is use for searching a pattern in a files or files . grep scan its input for a pattern and display the lines containing the pattern, line number , files which contain the pattern etc.
syntax:
grep [option] pattern file-name(s)
let we have file sandeep.lst contains following data
Example
1. grep "sandeep" sandeep.lst or grep sandeep sandeep.lst or grep 'sandeep' sandeep.lst
output:
*important pattern can be enclosed within single quotes or double quotes . One thing should be remember that if we are using special symbols (like *,?,[]) then double quotes should be used other wise if single quotes used then these symbol has no meaning.
* if possible always use double quotes.
grep options
1. ignoring case (-i) : searches for the pattern by ignoring the case .
Example
grep -i "sandeep" sandeep.lst
output:
2. deleting lines (-v) : select those lines which does not contain pattern
Example
grep -v "vaibhav" sandeep.lst
output:
3. displaying line number (-n) : This option display the line number of the line containing the pattern.
Example
grep -n "laxmi nagar" sandeep.lst
output:
4. counting lines containing pattern (-c) : This option display the number of times pattern occur.
Example
grep -c "laxmi nagar" sandeep.lst
output:
if -c option used with multiple files then
5. Matching multiple patterns (-e) : Multiple patterns can be matched using this option.
Example
grep -e "sandeep" -e "vaibhav" sandeep.lst
output:
grep can be used by combining different options together
Example
grep -vi "sandeep" sandeep.lst
*This command output all lines except lines containing sandeep whether it is in lower case , upper case or mixed.
output:
grep (global regular expression parse) is use for searching a pattern in a files or files . grep scan its input for a pattern and display the lines containing the pattern, line number , files which contain the pattern etc.
syntax:
grep [option] pattern file-name(s)
let we have file sandeep.lst contains following data
$ cat > sandeep.lst
sandeep | 028 | delhi cantt | 5000
vaibhav | 034 | laxmi nagar | 8000
anurah | 033 | sahadra | 9000
ankit | 014 | sahadra | 12000
parth | 024 | ip extension | 14000
udit | 007 | ip extension | 2000
anki | 051 | laxmi nagar | 5000
SANDEEP|098|rajghat|7000
^C
$
sandeep | 028 | delhi cantt | 5000
vaibhav | 034 | laxmi nagar | 8000
anurah | 033 | sahadra | 9000
ankit | 014 | sahadra | 12000
parth | 024 | ip extension | 14000
udit | 007 | ip extension | 2000
anki | 051 | laxmi nagar | 5000
SANDEEP|098|rajghat|7000
^C
$
Example
1. grep "sandeep" sandeep.lst or grep sandeep sandeep.lst or grep 'sandeep' sandeep.lst
output:
$ grep sandeep sandeep.lst
sandeep | 028 | delhi cantt | 5000
$ grep "sandeep" sandeep.lst
sandeep | 028 | delhi cantt | 5000
$ grep 'sandeep' sandeep.lst
sandeep | 028 | delhi cantt | 5000
$
sandeep | 028 | delhi cantt | 5000
$ grep "sandeep" sandeep.lst
sandeep | 028 | delhi cantt | 5000
$ grep 'sandeep' sandeep.lst
sandeep | 028 | delhi cantt | 5000
$
*important pattern can be enclosed within single quotes or double quotes . One thing should be remember that if we are using special symbols (like *,?,[]) then double quotes should be used other wise if single quotes used then these symbol has no meaning.
* if possible always use double quotes.
grep options
1. ignoring case (-i) : searches for the pattern by ignoring the case .
Example
grep -i "sandeep" sandeep.lst
output:
$ grep -i sandeep sandeep.lst
sandeep | 028 | delhi cantt | 5000
SANDEEP|098|rajghat|7000
$
sandeep | 028 | delhi cantt | 5000
SANDEEP|098|rajghat|7000
$
2. deleting lines (-v) : select those lines which does not contain pattern
Example
grep -v "vaibhav" sandeep.lst
output:
$ grep -v "vaibhav" sandeep.lst
sandeep | 028 | delhi cantt | 5000
anurah | 033 | sahadra | 9000
ankit | 014 | sahadra | 12000
parth | 024 | ip extension | 14000
udit | 007 | ip extension | 2000
anki | 051 | laxmi nagar | 5000
SANDEEP|098|rajghat|7000
$
sandeep | 028 | delhi cantt | 5000
anurah | 033 | sahadra | 9000
ankit | 014 | sahadra | 12000
parth | 024 | ip extension | 14000
udit | 007 | ip extension | 2000
anki | 051 | laxmi nagar | 5000
SANDEEP|098|rajghat|7000
$
3. displaying line number (-n) : This option display the line number of the line containing the pattern.
Example
grep -n "laxmi nagar" sandeep.lst
output:
$ grep -n "laxmi nagar" sandeep.lst
2:vaibhav | 034 | laxmi nagar | 8000
7:anki | 051 | laxmi nagar | 5000
$
2:vaibhav | 034 | laxmi nagar | 8000
7:anki | 051 | laxmi nagar | 5000
$
4. counting lines containing pattern (-c) : This option display the number of times pattern occur.
Example
grep -c "laxmi nagar" sandeep.lst
output:
$ grep -c "laxmi nagar" sandeep.lst
2
$
2
$
if -c option used with multiple files then
Example
grep -c "sandeep" sandeep*.lst
grep -c "sandeep" sandeep*.lst
output:
*above command searches all the files starting with sandeep in the current directory if that files contains the pattern then it display the result.
$ cat > sandeep2.lst
sandeep
vaibhav
^C
$ grep -c "sandeep" sandeep*.lst
sandeep.lst:1
sandeep2.lst:1
$
sandeep
vaibhav
^C
$ grep -c "sandeep" sandeep*.lst
sandeep.lst:1
sandeep2.lst:1
$
*above command searches all the files starting with sandeep in the current directory if that files contains the pattern then it display the result.
5. Matching multiple patterns (-e) : Multiple patterns can be matched using this option.
Example
grep -e "sandeep" -e "vaibhav" sandeep.lst
output:
$ grep -e "sandeep" -e "vaibhav" sandeep.lst
sandeep | 028 | delhi cantt | 5000
vaibhav | 034 | laxmi nagar | 8000
$
sandeep | 028 | delhi cantt | 5000
vaibhav | 034 | laxmi nagar | 8000
$
grep can be used by combining different options together
Example
grep -vi "sandeep" sandeep.lst
*This command output all lines except lines containing sandeep whether it is in lower case , upper case or mixed.
output:
$ grep -vi "sandeep" sandeep.lst
vaibhav | 034 | laxmi nagar | 8000
anurah | 033 | sahadra | 9000
ankit | 014 | sahadra | 12000
parth | 024 | ip extension | 14000
udit | 007 | ip extension | 2000
anki | 051 | laxmi nagar | 5000
$
vaibhav | 034 | laxmi nagar | 8000
anurah | 033 | sahadra | 9000
ankit | 014 | sahadra | 12000
parth | 024 | ip extension | 14000
udit | 007 | ip extension | 2000
anki | 051 | laxmi nagar | 5000
$
If You Liked This Post Please Take a Time To Share This Post
This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again.
ReplyDeleteevs full form
raw agent full form
full form of tbh in instagram
dbs bank full form
https full form
tft full form
pco full form
kra full form in hr
tbh full form in instagram story
epc full form
This amazing blog post is related to the grep command in linuxunix. Thanks for sharing this amazing blog post .
ReplyDeletehttps://www.lovingparents.in/kids/kids-5-12-years/7-common-childhood-illnesses-and-their-treatments/