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 Responses to “Short Tip: grep with more than one expression [Update]”

  1. Temet Says:

    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. elcuco Says:

    cat file | egrep ‘foo|bar’

  3. anon Says:

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

    you dont need the ‘cat’ :)

  4. Jos Says:

    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

  5. uhuu Says:

    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.

  6. joshuadf Says:

    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.

  7. Kaze Says:

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

  8. arab Says:

    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.

  9. Daniel Says:

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

    Daniel

  10. litb Says:

    joshuadf: thanks for that great tip :)

  11. Jan Kundrát Says:

    Hi liquidat, you’ve just won the useless use of cat award. Congratulations :p.

  12. Allen Halsey Says:

    Hi Jan,

    You’ve just won the giddy unix user award. Congratulations :p.

    @liquidat: thanks for the grep tip.

  13. Adam Says:

    You can also do: grep -E ‘foo|bar’

  14. lynx Says:

    no need for extended grep:
    grep “foo\|bar\|baz” file

  15. Constantin Says:

    Thanks, I’ve been looking for a way to do this.


Comments are closed.

Follow

Get every new post delivered to your Inbox.

Join 84 other followers

%d bloggers like this: