Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Grep search on noncontiguous words

  1. #1
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Was grep search. Now awk equivalent command.

    I need to use grep to search for lines that contain two words that are not contiguous. I finally got this to work by piping one grep search into another somewhat as follows:

    Code:
    grep firstword file.txt | grep secondword
    Is there an easier or more direct way to do this? I did a google search and read the grep man page but didn't find anything. I thought a regular expression might do the trick but couldn't find anything helpful on this either.
    Last edited by kaibob; March 12th, 2009 at 02:44 AM.

  2. #2
    Join Date
    May 2007
    Location
    Canada
    Beans
    581

    Re: Grep search on noncontiguous words

    you want to use an OR command, so if it finds word1 OR word2 it will output those lines...
    Code:
    cat file | grep "word1\|word2"


    64-Bit Ubuntu! Join the future of computing.

  3. #3
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Grep search on noncontiguous words

    Quote Originally Posted by cubeist View Post
    you want to use an OR command, so if it finds word1 OR word2 it will output those lines...
    Code:
    cat file | grep "word1\|word2"
    Sorry--I didn't explain properly. I want grep to find lines that contain both words. So, it would be word1 AND word2 and, as previously mentioned, these words are located on the same line but are separated from one another by other words.

  4. #4
    Join Date
    May 2007
    Location
    Canada
    Beans
    581

    Re: Grep search on noncontiguous words

    no prob, the AND operator is the ampersand &


    64-Bit Ubuntu! Join the future of computing.

  5. #5
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Grep search on noncontiguous words

    Quote Originally Posted by kaibob View Post
    Code:
    cat file.txt | grep firstword | grep secondword
    Is there an easier or more direct way to do this?
    use awk, and stop using useless cat. tools like grep/awk/wc/cut etc can take in file input.
    Code:
    awk '/firstword/ && /secondword' file.txt

  6. #6
    Join Date
    Jun 2006
    Beans
    2,930

    Re: Grep search on noncontiguous words

    Quote Originally Posted by ghostdog74 View Post
    use awk, and stop using useless cat. tools like grep/awk/wc/cut etc can take in file input.
    Code:
    awk '/firstword/ && /secondword' file.txt
    you don't have to use cat to use grep:
    Code:
    grep firstword file.txt| grep secondword
    I find awk more painful to learn, but there are times when it does seem like the best option

  7. #7
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Grep search on noncontiguous words

    Quote Originally Posted by lavinog View Post
    you don't have to use cat to use grep:
    Code:
    grep firstword file.txt| grep secondword
    I find awk more painful to learn, but there are times when it does seem like the best option
    grep does one thing in a specific area, awk does more than that.

  8. #8
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Grep search on noncontiguous words

    cubeist and ghostdog74. Thanks for the responses. I'm new to shell scripting but am making progress.

  9. #9
    Join Date
    May 2007
    Location
    Canada
    Beans
    581

    Re: Grep search on noncontiguous words

    Quote Originally Posted by kaibob View Post
    cubeist and ghostdog74. Thanks for the responses. I'm new to shell scripting but am making progress.
    No problem, I agree with ghost dog, when you can, get into the habit of using more advanced programs like sed and awk. Then in the future, when you are writing more complex scripts, you can go back and use your old sed, awk commands and expand on them...

    grep is also a fine tool, but it is usually better if what you are trying to do can be accomplished with one command instead of two... this leads to cleaner code and better recycle-ability...


    64-Bit Ubuntu! Join the future of computing.

  10. #10
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Grep search on noncontiguous words

    Quote Originally Posted by cubeist View Post
    No problem, I agree with ghost dog, when you can, get into the habit of using more advanced programs like sed and awk.
    (g)awk can be used to do the jobs of some of these commonly used tools:
    1) sed
    2) grep
    3) wc
    4) cut
    5) tr
    6) uniq
    7) sort
    therefore, learning awk greatly reduces one's learning curve.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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