Re: grep with multiple and



On Wednesday 4 February 2009 17:05, marc wrote:

Hello,

Is it possible with grep to find multiple strings in a file ?
i.e. : file foo.txt contains : S1 and S2 and S3 and S4, etc...

I've found with 2 values , but not with several ones.

It's not clear to me what you're asking, however:

grep -e S1 -e S2 -e S3 -e S4 file

will match line containing S1 OR S2 OR S3 OR S4, while

grep 'S1.*S2.*S3.*S4' file

matches lines that contains S1 AND S2 AND S3 AND S4, in that order. If you
want to match for all patterns (AND) in any order, you can either write an
insanely long grep command line, or use awk:

awk '/S1/&&/S2/&&/S3/&&/S4/' file
.



Relevant Pages

  • Re: General UNIX puzzle
    ... All of those (sed, awk, grep) involve making a copy of the original file ... The gnu shtool is one of the most useful tools for this for in-place ... A Galileo could no more be elected president of the United States than ...
    (freebsd-questions)
  • Re: ssh and chroot
    ... ++ echo /bin/mkdir ... ++ awk '' ... ++ grep '^/' ...
    (comp.security.ssh)
  • Re: The smartest band in the world?
    ... I mainly use grep, but I'm ... going to learn awk and sed and improve my bash scripting. ... Oracle accreditation coure, well in the grad courses at least, everything ...
    (alt.guitar)
  • Re: Difference between Cygwin and DOS handling of string input
    ... Ton of userland tools that make work possible (perl, sed, awk, grep, ... cmd.exe is a shell in that it's a TTY that lets you run commands, ...
    (comp.lang.c)
  • Re: The smartest band in the world?
    ... I mainly use grep, but I'm going to learn awk and sed and improve my bash scripting. ... mostly taching Oracle and at the subjects completion you can do the Oracle accreditation coure, well in the grad courses at least, everything seems to have a proprietry product focus. ...
    (alt.guitar)