• how to grep for pattern1 AND pattern2 in a file

    From Adam@adam@no_thanks.com to alt.os.linux.ubuntu on Thu Mar 13 11:17:03 2025
    From Newsgroup: alt.os.linux.ubuntu


    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.
    --- Synchronet 3.20c-Linux NewsLink 1.2
  • From Paul@nospam@needed.invalid to alt.os.linux.ubuntu on Thu Mar 13 15:16:01 2025
    From Newsgroup: alt.os.linux.ubuntu

    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
    --- Synchronet 3.20c-Linux NewsLink 1.2
  • From Adam@adam@no_thanks.com to alt.os.linux.ubuntu on Thu Mar 13 12:56:42 2025
    From Newsgroup: alt.os.linux.ubuntu

    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.

    --- Synchronet 3.20c-Linux NewsLink 1.2
  • From Lem Novantotto@Lem@none.invalid to alt.os.linux.ubuntu on Thu Mar 13 20:44:12 2025
    From Newsgroup: alt.os.linux.ubuntu

    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
    --- Synchronet 3.20c-Linux NewsLink 1.2
  • From Paul@nospam@needed.invalid to alt.os.linux.ubuntu on Thu Mar 13 16:44:49 2025
    From Newsgroup: alt.os.linux.ubuntu

    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
    --- Synchronet 3.20c-Linux NewsLink 1.2
  • From Adam@adam@no_thanks.com to alt.os.linux.ubuntu on Thu Mar 13 14:38:45 2025
    From Newsgroup: alt.os.linux.ubuntu

    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. ========================================================================


    --- Synchronet 3.20c-Linux NewsLink 1.2
  • From Dan Purgert@dan@djph.net to alt.os.linux.ubuntu on Fri Mar 14 01:20:58 2025
    From Newsgroup: alt.os.linux.ubuntu

    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
    --- Synchronet 3.20c-Linux NewsLink 1.2
  • From rbowman@bowman@montana.com to alt.os.linux.ubuntu on Fri Mar 14 02:58:58 2025
    From Newsgroup: alt.os.linux.ubuntu

    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.

    --- Synchronet 3.20c-Linux NewsLink 1.2
  • From Jonathan N. Little@lws4art@gmail.com to alt.os.linux.ubuntu on Fri Mar 14 09:30:15 2025
    From Newsgroup: alt.os.linux.ubuntu

    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.


    In this instance I just use cat:

    (stdout)
    $ cat sample.txt | grep cat | grep dog
    I am a cat or a dog

    BTW note on egrep deprecation:

    <https://www.phoronix.com/news/GNU-Grep-3.8-Stop-egrep-fgrep>
    --
    Take care,

    Jonathan
    -------------------
    LITTLE WORKS STUDIO
    http://www.LittleWorksStudio.com
    --- Synchronet 3.20c-Linux NewsLink 1.2
  • From Allodoxaphobia@trepidation@example.net to alt.os.linux.ubuntu on Fri Mar 14 13:40:53 2025
    From Newsgroup: alt.os.linux.ubuntu

    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
    --- Synchronet 3.20c-Linux NewsLink 1.2
  • From Mark Bourne@nntp.mbourne@spamgourmet.com to alt.os.linux.ubuntu on Fri Mar 14 19:45:34 2025
    From Newsgroup: alt.os.linux.ubuntu

    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.
    --- Synchronet 3.20c-Linux NewsLink 1.2
  • From Lawrence D'Oliveiro@ldo@nz.invalid to alt.os.linux.ubuntu on Sun Mar 16 00:36:06 2025
    From Newsgroup: alt.os.linux.ubuntu

    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.
    --- Synchronet 3.20c-Linux NewsLink 1.2
  • From red floyd@no.spam.here@its.invalid to alt.os.linux.ubuntu on Mon Mar 17 20:51:30 2025
    From Newsgroup: alt.os.linux.ubuntu

    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

    --- Synchronet 3.20c-Linux NewsLink 1.2
  • From red floyd@no.spam.here@its.invalid to alt.os.linux.ubuntu on Mon Mar 17 20:52:55 2025
    From Newsgroup: alt.os.linux.ubuntu

    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 .

    --- Synchronet 3.20c-Linux NewsLink 1.2