Short Tip: grep with more than one expression [Update]

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

About these ads

15 thoughts on “Short Tip: grep with more than one expression [Update]

  1. I have always been told that “cat blabla | grep pouet” is bad.

    “grep pouet blabla” is good.

    … for me, the result is the same ^^

  2. ~> grep –help
    Usage: grep [OPTION]… PATTERN [FILE] …
    Search for PATTERN in each FILE or standard input.

    you dont need the ‘cat’ :)

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

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

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

  6. To Anon : the result is not the same afaik, because you have two processes for cat and grep, while only one with grep alone.

  7. I actually use egrep for serching for more than one term, fe:

    dmesg | egrep “(ati|vga)”

    Also using regexp’s may aid you in getting better results.

  8. I would alway use egrep for more than one expression.
    more than one -e in grep doesn’t work on a all architectures.

    Daniel

Comments are closed.