How do I use grep command in Linux?
grep command searches the given file for lines containing a match to the given strings or words. By default, grep prints the matching lines. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines.
The name, "grep", derives from the command used to perform a similar operation, using the Unix/Linux text editor ed:
g/re/p
grep command syntax
grep 'word' filename grep 'string1 string2' filename cat otherfile | grep 'something' command | grep 'something'
Use grep to search file
Search /etc/passwd for boo user:
$ grep boo /etc/passwd
You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with -i option:
$ grep -i "boo" /etc/passwd
Use grep recursively
You can search recursively i.e. read all files under each directory for a string "192.168.1.5"
$ grep -r "192.168.1.5" /etc/
Use grep to search words only
When you search for boo, grep will match fooboo, boo123, etc. You can force grep to select only those lines containing matches that form whole words i.e. match only boo word:
$ grep -w "boo" /path/to/file
Use grep to search 2 different words
use egrep as follows:
$ egrep -w 'word1|word2' /path/to/file
Count line when words has been matched
grep can report the number of times that the pattern has been matched for each file using -c (count) option:
$ grep -c 'word' /path/to/file
Also note that you can use -n option, which causes grep to precede each line of output with the number of the line in the text file from which it was obtained:
$ grep -n 'word' /path/to/file
Grep invert match
You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar:
$ grep -v bar /path/to/file
UNIX / Linux pipes and grep command
grep command often used with pipes. For example print name of hard disk devices:
# dmesg | egrep '(s|h)d[a-z]'
Display cpu model name:
# cat /proc/cpuinfo | grep -i 'Model'
However, above command can be also used as follows without shell pipe:
# grep -i 'Model' /proc/cpuinfo
How do I list just the names of matching files?
Use the -l option to list file name whose contents mention main():
$ grep -l 'main' *.c
Finally, you can force grep to display output in colors:
$ grep --color vivek /etc/passwd
- Email FAQ to a friend
- Printable version
- Rss Feed
- Last Updated: 6-3-09


{ 26 comments… read them below or add one }
I found this tutorial being the most clear and helpful one. Thank you.
This Tutorial is so far helpful to me.
Thank you..!!
what if i want to search keyword
like “$this->Products” or “['status']” ?
doesnt work
Try
this is very useful to me.. thanks …
Very helpful. saved my time.
What is the best way to grep recursively through a directory tree, and display the pattern matches, that occur in just all the *.cpp files?
For example:
grep -HRn “look for this” *.cpp
doesn’t work (on Linux)
what if i want to search like this
Hi
I want to search t1.spq, t2.spq ….. tn.spq words from a file
grep -i “*.spq” filename doesn’t work
Please tell how can search such words??
-Regards,
tauqueer
avoid using * grep -i “spq” tt.sh
then you will get all the words which will have spq.
if some thing need to refrained from that need to get the desired out put then use
grep -i “spq” tt.sh | grep -v ” somepattern”
How can i grep for an output that come after a statement: eg Expires: 10/May/2009.
If i want to caputure only the date, how can i grep for what comes after the colon (:)
please help
Try,
echo 'Expires: 10/May/2009' | cut -d: -f2OR
echo 'Expires: 10/May/2009' | awk -F':' '{ print $2}'This is quite informative…… Thanks.
Also I have a question, what is the expansion of ‘grep’? Can anyone answer?
This helps a lot,,
Rizvana,
grep means Get Regular Expression and Print
How can i do calculation on dates;
eg to know the number of days between ‘todays date’ and a day like ‘15/may/2009′
please help
thanks .i gt the rite information.
Hi,
How can I use grep function to search files that file name with “ord” or “rec” from specific dir??
Hi
lets say i have some data :
a
a
a
b
b
b
c
c
c
can I use grep comand to make like this :
a
b
c
thanks
Try uniq command.
Vivek,
its working…..thanks a lot
Hi,
No one know how to use grep function to search files that file name with “ord” or “rec” from specific dir??
Try
Hi,
I’d like to get the total cpu and memory usage easily and I think of using ‘dstat’ command. Can I get the values corresponding to the free and used column with grep?
------memory-usage-----
used buff cach free
153M 876k 24M 4392k
cheers!
The Most Helpful POST
For those who want to search files with wild cards and the like, try the find command with -exec.
find /dir/to/search/ -iname *.cpp -exec grep 'word' '{}' \;and snake, I do not think it is possible to search columns with grep, I’m 98% sure that it is line (row) only.