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,247 of 134,474   
   Mark Bourne to Adam   
   Re: how to grep for pattern1 AND pattern   
   14 Mar 25 19:45:34   
   
   From: nntp.mbourne@spamgourmet.com   
      
   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-fil   
   -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-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