#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    5
    Rep Power
    0

    System call of grep


    Hi there,
    I wish to use a system call of grep but unable to grep for multiple words though if I tried it from command line it worked fine. The syntax for multiple words grep is:
    # grep 'word1\|word2' filename.txt
    PHP Code:
    // This statement works perfectly
    system("grep 'word' filename.txt");

    // None of these works
    system("grep 'word1\|word2' filename.txt");
    system("grep 'word1\\|word2' filename.txt");
    system("grep 'word1/\|word2' filename.txt");
    system("grep 'word1&92;&124;word2' filename.txt"); 
    Please help. Thanks in advance.
    --ngungo
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,746
    Rep Power
    9397
    Originally Posted by ngungo
    The syntax for multiple words grep is:
    # grep 'word1\|word2' filename.txt
    Uh, no? It's
    Code:
    grep 'word1|word2' filename.txt
    The backslash you added escaped the pipe, thus grep would literally look for "word1|word2".

    Why they don't work:
    1. Backslash
    2. Backslash (you escaped the backslash in PHP)
    3. Backslash still, but now there's a forward slash in there for some reason
    4. Putting aside the fact that those aren't actually valid HTML entities, why the heck would HTML entities give you the result you need? Do you know why they're called "HTML entities"?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    5
    Rep Power
    0
    Thanks requinix. For quick response.
    I have checked carefully and many times that
    Code:
    grep 'word1|word2' filename.txt
    would not work. It's syntactically incorrect.
    Here is some reference: http://www.cyberciti.biz/faq/searching-multiple-words-string-using-grep/



    Code:
    grep 'word1\|word2' filename.txt
    or
    Code:
    egrep 'word1|word2' filename.txt
    would work.

    Comments on this post

    • requinix agrees
  6. #4
  7. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,746
    Rep Power
    9397
    You can't see me but I have egg on my face. So I may have aliased grep to egrep a long time ago, which would explain why the "master|worker" I searched for today worked. And why I thought that grep worked with the more powerful regular expressions by default and not the ones where | is a simple character and \| is the metacharacter.

    Which leaves me with nothing to explain why it doesn't seem to work for you. Particularly when I try it myself.
    Code:
    $ cat test.txt
    foo
    bar
    baz
    $ cat test.php
    <?php
    
    system("grep 'foo\|bar' test.txt");
    
    $ php -f test.php
    foo
    bar
    $
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    5
    Rep Power
    0
    Thanks!
    Solved!

    The error I got was in a different aspect.

IMN logo majestic logo threadwatch logo seochat tools logo