home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   alt.os.linux.ubuntu      I preferred Xubuntu, seemed a bit faster      134,474 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 134,244 of 134,474   
   Dan Purgert to Adam   
   Re: how to grep for pattern1 AND pattern   
   14 Mar 25 01:20:58   
   
   From: dan@djph.net   
      
   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-DOS v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca