• Re: how to grep for pattern1 AND pattern2 in a file

    From Lawrence D'Oliveiro@21:1/5 to Jonathan N. Little on Sun Mar 16 00:36:06 2025
    On Fri, 14 Mar 2025 09:30:15 -0400, Jonathan N. Little wrote:

    BTW note on egrep deprecation:

    grep -E for egrep, grep -F for fgrep, it’s all grep now.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From red floyd@21:1/5 to red floyd on Mon Mar 17 20:52:55 2025
    On 3/17/2025 8:51 PM, red floyd wrote:
    On 3/13/2025 7:58 PM, rbowman wrote:
    [redacted]

    As a variation on that theme:

    find . -name "*.c" | xargs grep foobar

    Not what you asked for but handy.


    For your specific example

    grep -r --include='*.c' foobar


    CURSES...

    grep -r --include='*.c' foobar .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From red floyd@21:1/5 to rbowman on Mon Mar 17 20:51:30 2025
    On 3/13/2025 7:58 PM, rbowman wrote:
    [redacted]

    As a variation on that theme:

    find . -name "*.c" | xargs grep foobar

    Not what you asked for but handy.


    For your specific example

    grep -r --include='*.c' foobar

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Adam@21:1/5 to All on Thu Mar 13 11:17:03 2025
    The following ...

    grep -e pattern1 -e pattern2 wget_output_file.txt

    is NOT giving the result that I'm looking for, which is BOTH patterns
    must be in the file.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Adam@21:1/5 to Paul on Thu Mar 13 12:56:42 2025
    On 03/13/2025 12:16 PM, Paul wrote:
    On Thu, 3/13/2025 2:17 PM, Adam wrote:

    The following ...

    grep -e pattern1 -e pattern2 wget_output_file.txt

    is NOT giving the result that I'm looking for, which is BOTH patterns must be in the file.

    "The egrep variant supports an extended regular expression syntax added by Alfred Aho
    after Ken Thompson's original regular expression implementation.[12]

    The "fgrep" variant searches for any of a list of fixed strings using
    the Aho–Corasick string matching algorithm."

    *******

    sample.txt
    ----------
    I am a cat
    I am a dog
    I am a cat or a dog
    I am an elephant

    (stdout)

    $ egrep "cat|dog" sample.txt
    I am a cat
    I am a dog
    I am a cat or a dog

    "If at first you don't grep, try try again... with a egrep or a fgrep" :-)

    It's a good thing these are documented.

    Paul


    I'm looking for AND (not OR) operator. So, both patterns must be in the
    file (not line) or FALSE is returned.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul@21:1/5 to Adam on Thu Mar 13 15:16:01 2025
    On Thu, 3/13/2025 2:17 PM, Adam wrote:

    The following ...

    grep -e pattern1 -e pattern2 wget_output_file.txt

    is NOT giving the result that I'm looking for, which is BOTH patterns must be in the file.

    "The egrep variant supports an extended regular expression syntax added by Alfred Aho
    after Ken Thompson's original regular expression implementation.[12]

    The "fgrep" variant searches for any of a list of fixed strings using
    the Aho–Corasick string matching algorithm."

    *******

    sample.txt
    ----------
    I am a cat
    I am a dog
    I am a cat or a dog
    I am an elephant

    (stdout)

    $ egrep "cat|dog" sample.txt
    I am a cat
    I am a dog
    I am a cat or a dog

    "If at first you don't grep, try try again... with a egrep or a fgrep" :-)

    It's a good thing these are documented.

    Paul

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul@21:1/5 to Adam on Thu Mar 13 16:44:49 2025
    On Thu, 3/13/2025 3:56 PM, Adam wrote:
    On 03/13/2025 12:16 PM, Paul wrote:
    On Thu, 3/13/2025 2:17 PM, Adam wrote:

    The following ...

    grep -e pattern1 -e pattern2 wget_output_file.txt

    is NOT giving the result that I'm looking for, which is BOTH patterns must be in the file.

    "The egrep variant supports an extended regular expression syntax added by Alfred Aho
      after Ken Thompson's original regular expression implementation.[12]

      The "fgrep" variant searches for any of a list of fixed strings using
      the Aho–Corasick string matching algorithm."

    *******

    sample.txt
    ----------
    I am a cat
    I am a dog
    I am a cat or a dog
    I am an elephant

    (stdout)

    $ egrep "cat|dog" sample.txt
    I am a cat
    I am a dog
    I am a cat or a dog

    "If at first you don't grep, try try again... with a egrep or a fgrep" :-) >>
    It's a good thing these are documented.

         Paul


    I'm looking for AND (not OR) operator. So, both patterns must be in the file (not line) or FALSE is returned.


    sample.txt
    ----------

    I am a cat
    I am a dog
    I am a cat and a dog
    I am a dog and a cat
    I am an elephant

    I had to use CoPilot, as my crafting experiments all failed.

    $ egrep 'cat.*dog|dog.*cat' sample.txt # cat followed by dog or dog followed by cat
    I am a cat and a dog
    I am a dog and a cat
    $

    Note that, this needs to be rigorously tested.
    Just based on my failures before asking CoPilot,
    you can't just assume this actually works...

    I am a cat
    I am a dog
    I am a cat and a dog
    I am a dog and a cat
    I am a dog dog and a cat
    I am a dog dog and a cat dog
    I am a dog dog and a cat cat
    I am a dog cat and a dog cat
    I am a dog cat and a cat dog
    I am an elephant

    $ egrep 'cat.*dog|dog.*cat' sample.txt
    I am a cat and a dog
    I am a dog and a cat
    I am a dog dog and a cat
    I am a dog dog and a cat dog
    I am a dog dog and a cat cat
    I am a dog cat and a dog cat
    I am a dog cat and a cat dog
    $

    I would imagine this technique rapidly runs out of legs.

    Paul

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lem Novantotto@21:1/5 to All on Thu Mar 13 20:44:12 2025
    Il Thu, 13 Mar 2025 12:56:42 -0700, Adam ha scritto:

    I'm looking for AND (not OR) operator. So, both patterns must be in the
    file (not line) or FALSE is returned.

    $ ( grep -l pattern1 file | xargs grep -l pattern2 ) || false

    You can do it better. Read the manual of the involved commands, if you
    like.
    --
    Bye, Lem
    Talis erit dies qualem egeris

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Adam@21:1/5 to Lem Novantotto on Thu Mar 13 14:38:45 2025
    On 03/13/2025 01:44 PM, Lem Novantotto wrote:
    Il Thu, 13 Mar 2025 12:56:42 -0700, Adam ha scritto:

    I'm looking for AND (not OR) operator. So, both patterns must be in the
    file (not line) or FALSE is returned.

    $ ( grep -l pattern1 file | xargs grep -l pattern2 ) || false

    You can do it better. Read the manual of the involved commands, if you
    like.


    Thanks, your post led me to...

    How can I search a file to see if it contains multiple patterns anywhere
    in the file? https://askubuntu.com/questions/1501703/how-can-i-search-a-file-to-see-if-it-contains-multiple-patterns-anywhere-in-the
    ======================================================================== waltinator's answer...

    It can be done using multiple grep commands. Read man grep xargs, and do something like

    grep -l 'pattern1' -f filelist | \
    xargs grep -l 'pattern2` | \
    xargs grep -l 'pattern3'

    The first grep produces a list of files containing the first pattern.
    The second (xargs grep) searches for the second pattern in the files
    containing the first pattern. ========================================================================

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dan Purgert@21:1/5 to Adam on Fri Mar 14 01:20:58 2025
    On 2025-03-13, Adam wrote:
    On 03/13/2025 12:16 PM, Paul wrote:
    On Thu, 3/13/2025 2:17 PM, Adam wrote:

    The following ...

    grep -e pattern1 -e pattern2 wget_output_file.txt

    is NOT giving the result that I'm looking for, which is BOTH
    patterns must be in the file.

    "The egrep variant supports an extended regular expression syntax
    added by Alfred Aho after Ken Thompson's original regular expression
    implementation.[12]

    The "fgrep" variant searches for any of a list of fixed strings
    using the Aho–Corasick string matching algorithm."

    *******

    sample.txt
    ----------
    I am a cat
    I am a dog
    I am a cat or a dog
    I am an elephant

    (stdout)

    $ egrep "cat|dog" sample.txt
    I am a cat
    I am a dog
    I am a cat or a dog

    "If at first you don't grep, try try again... with a egrep or a fgrep" :-) >>
    It's a good thing these are documented.

    Paul


    I'm looking for AND (not OR) operator. So, both patterns must be in the
    file (not line) or FALSE is returned.


    So you'll need something like this, or a different language.

    if grep -q PATTERN1 theFile; then
    if grep -q PATTERN2 theFile; then
    printf "Both patterns contained in file.\n"
    else
    printf "Pattern2 missing from file.\n"
    fi
    else
    printf "Pattern1 missing from file.\n"
    fi


    could probably be cleaned up a bit, but it'll do what you want.

    --
    |_|O|_|
    |_|_|O| Github: https://github.com/dpurgert
    |O|O|O| PGP: DDAB 23FB 19FA 7D85 1CC1 E067 6D65 70E5 4CE7 2860

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From rbowman@21:1/5 to Adam on Fri Mar 14 02:58:58 2025
    On Thu, 13 Mar 2025 14:38:45 -0700, Adam wrote:

    On 03/13/2025 01:44 PM, Lem Novantotto wrote:
    Il Thu, 13 Mar 2025 12:56:42 -0700, Adam ha scritto:

    I'm looking for AND (not OR) operator. So, both patterns must be in
    the file (not line) or FALSE is returned.

    $ ( grep -l pattern1 file | xargs grep -l pattern2 ) || false

    You can do it better. Read the manual of the involved commands, if you
    like.


    Thanks, your post led me to...

    How can I search a file to see if it contains multiple patterns anywhere
    in the file? https://askubuntu.com/questions/1501703/how-can-i-search-a-file-to-see-
    if-it-contains-multiple-patterns-anywhere-in-the
    ======================================================================== waltinator's answer...

    It can be done using multiple grep commands. Read man grep xargs, and do something like

    grep -l 'pattern1' -f filelist | \
    xargs grep -l 'pattern2` | \
    xargs grep -l 'pattern3'

    The first grep produces a list of files containing the first pattern.
    The second (xargs grep) searches for the second pattern in the files containing the first pattern. ========================================================================

    As a variation on that theme:

    find . -name "*.c" | xargs grep foobar

    Not what you asked for but handy.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Allodoxaphobia@21:1/5 to rbowman on Fri Mar 14 13:40:53 2025
    On 14 Mar 2025 02:58:58 GMT, rbowman wrote:
    On Thu, 13 Mar 2025 14:38:45 -0700, Adam wrote:

    On 03/13/2025 01:44 PM, Lem Novantotto wrote:
    Il Thu, 13 Mar 2025 12:56:42 -0700, Adam ha scritto:

    I'm looking for AND (not OR) operator. So, both patterns must be in
    the file (not line) or FALSE is returned.

    $ ( grep -l pattern1 file | xargs grep -l pattern2 ) || false

    You can do it better. Read the manual of the involved commands, if you
    like.

    Thanks, your post led me to...

    How can I search a file to see if it contains multiple patterns anywhere
    in the file?
    https://askubuntu.com/questions/1501703/how-can-i-search-a-file-to-see-
    if-it-contains-multiple-patterns-anywhere-in-the
    ========================================================================
    waltinator's answer...

    It can be done using multiple grep commands. Read man grep xargs, and do
    something like

    grep -l 'pattern1' -f filelist | \
    xargs grep -l 'pattern2` | \
    xargs grep -l 'pattern3'

    The first grep produces a list of files containing the first pattern.
    The second (xargs grep) searches for the second pattern in the files
    containing the first pattern.
    ========================================================================

    As a variation on that theme:

    find . -name "*.c" | xargs grep foobar

    Not what you asked for but handy.

    _IF_ your 2 text strings occur in a specific sequence

    $ grep "STRING1.*STRING2" FILE.NM

    Jonesy
    --
    Marvin L Jones | Marvin | W3DHJ | linux
    Pueblo, Colorado | @ | Jonesy | FreeBSD __
    38.238N 104.547W | jonz.net | DM78rf | SK

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mark Bourne@21:1/5 to Adam on Fri Mar 14 19:45:34 2025
    Adam wrote:
    On 03/13/2025 01:44 PM, Lem Novantotto wrote:
    Il Thu, 13 Mar 2025 12:56:42 -0700, Adam ha scritto:

    I'm looking for AND (not OR) operator. So, both patterns must be in the
    file (not line) or FALSE is returned.

    $ ( grep -l pattern1 file | xargs grep -l pattern2 ) || false

    You can do it better. Read the manual of the involved commands, if you
    like.


    Thanks, your post led me to...

    How can I search a file to see if it contains multiple patterns anywhere
    in the file? https://askubuntu.com/questions/1501703/how-can-i-search-a-file-to-see-if-it-contains-multiple-patterns-anywhere-in-the

    ======================================================================== waltinator's answer...

    It can be done using multiple grep commands. Read man grep xargs, and do something like

    grep -l 'pattern1' -f filelist | \
        xargs grep -l 'pattern2` | \
        xargs grep -l 'pattern3'

    The first grep produces a list of files containing the first pattern.
    The second (xargs grep) searches for the second pattern in the files containing the first pattern. ========================================================================

    If there's a possibility that filenames might contain spaces (or other whitespace characters), it's safer to tell grep to separate each file in
    its output with null characters (instead of the default line break) and
    xargs to expect null as a separator in its input (instead of the default whitespace):

    grep -l 'pattern1' -f filelist -Z | \
    xargs -0 grep -l 'pattern2' -Z | \
    xargs -0 grep -l 'pattern3'

    I've left the "-Z" off the last grep, so that it outputs to the terminal
    one file per line. That might be confusing if filenames contain line
    break characters, which is allowed, but that would be unusual and if it
    does happen a human reader can probably work it out.

    --
    Mark.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)