Results 1 to 3 of 3
  1. #1
    Join Date
    May 2004
    Posts
    2

    Unanswered: Basic grep question...

    Folx,

    Very basic stuff... What is the correct way to grep lines with 2 particular strings in it?

    e.g. egrep "string1&string2" myfile

    Using egrep? I can do it with "or" but not with "and"... With "or" it looks like:

    egrep "string1|string2" myfile

    thx!

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    egrep 'string2.*string1|string1.*string2' file
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  3. #3
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    You can't really do an AND operation.

    If you know the order you can obviously do...

    grep "pattern1.*pattern2" yourFile

    If you don't know the order, you can do...

    grep "pattern1" yourFile | grep "pattern2"

    The above example is RUBBISH, so don't use unless your being really lazy!

    I'd use awk instead...

    awk '/pattern1/ && /pattern2/' yourFile

    or maybe even sed...

    sed -n '/pattern1/{ /pattern2/p; }' yourFile

    Damian

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •