
One of the main tools on th command line is grep. It is most often used to search for specific strings in program output, text files and so on. For the sake of an example, the syntax to search a the X.Org log for the string nvidia is:
# cat /var/log/Xorg.0.log|grep -i nvidia (--) PCI:*(0@1:0:0) nVidia Corporation Quadro NVS 135M rev 161, Mem @ 0xfd000000/0, 0xe0000000/0, 0xfa000000/0, I/O @ 0x0000ef00/0, BIOS @ 0x????????/131072 (II) NV: driver for NVIDIA chipsets: RIVA 128, RIVA TNT, RIVA TNT2, (--) NV: Found NVIDIA Quadro NVS 135M at 01@00:00:0
The -i flags the search as case-insensitive. That way you can only search for one expression a time. In case you want to search for more expressions at the same time, the option -e is needed:
# cat /var/log/Xorg.0.log|grep -i -e TNT -e nvidia
(--) PCI:*(0@1:0:0) nVidia Corporation Quadro NVS 135M rev 161, Mem @ 0xfd000000/0, 0xe0000000/0, 0xfa000000/0, I/O @ 0x0000ef00/0, BIOS @ 0x????????/131072
(II) NV: driver for NVIDIA chipsets: RIVA 128, RIVA TNT, RIVA TNT2,
Unknown TNT2, Vanta, RIVA TNT2 Ultra, RIVA TNT2 Model 64,
Aladdin TNT2, GeForce 256, GeForce DDR, Quadro, GeForce2 MX/MX 400,
(--) NV: Found NVIDIA Quadro NVS 135M at 01@00:00:0
This is a typical problem I often thought about but never got around to look it up. And there are surprisingly few people out there who now that feature! And yes, I know that cat should not be used that way, but it’s hard to forget once you’ve learned it wrong.
Update
I learned, and thus ususally use egrep these days instead of egrep with several options. There is now a short tip about egrep on this blog as well.

October 8, 2008 at 11:33
I have always been told that “cat blabla | grep pouet” is bad.
“grep pouet blabla” is good.
… for me, the result is the same ^^
October 8, 2008 at 11:47
cat file | egrep ‘foo|bar’
October 8, 2008 at 12:48
~> grep –help
Usage: grep [OPTION]… PATTERN [FILE] …
Search for PATTERN in each FILE or standard input.
you dont need the ‘cat’
October 8, 2008 at 13:00
great! I was always doing
grep foo file| grep bar |grep alice|grep -v bob
this now simplifies to
grep -e foo -e bar -e alice file |grep -v bob
October 8, 2008 at 13:11
Jos, you misunderstood:
grep -e foo -e bar file
matches any line in file that has foo OR bar in it. While
grep foo file | grep bar
of courses matches only lines that have both foo AND bar in them.
October 8, 2008 at 16:11
Another unrelated but good grep hint is something like:
ps auxww| grep ‘[j]ava’
Which will match actual java commands but not the grep command itself. Very handy when trying to kill off some.
October 8, 2008 at 16:18
To Anon : the result is not the same afaik, because you have two processes for cat and grep, while only one with grep alone.
October 8, 2008 at 16:43
I actually use egrep for serching for more than one term, fe:
Also using regexp’s may aid you in getting better results.
October 8, 2008 at 17:19
I would alway use egrep for more than one expression.
more than one -e in grep doesn’t work on a all architectures.
Daniel
October 8, 2008 at 17:43
joshuadf: thanks for that great tip
October 8, 2008 at 17:43
Hi liquidat, you’ve just won the useless use of cat award. Congratulations :p.
October 9, 2008 at 2:22
Hi Jan,
You’ve just won the giddy unix user award. Congratulations :p.
@liquidat: thanks for the grep tip.
October 9, 2008 at 13:33
You can also do: grep -E ‘foo|bar’
October 9, 2008 at 15:51
no need for extended grep:
grep “foo\|bar\|baz” file
October 10, 2008 at 2:42
Thanks, I’ve been looking for a way to do this.