Score:0

how to grep with multiple strings to find

es flag

I understand how to use grep in the simple form:

<command that spits out text> | grep "text to find"

I would like to be able to grep multiple different bits of text all at once. How do I do that? Is grep the correct command to do this?

Example

I run arp-scan and I get a list of devices and their mac addresses. I want to search for the presence of multiple unique mac address strings. If I only wanted 1 mac address, I would use grep like this:

arp-scan --localnet --interface=<my interface> | grep "mac address"

I have heard of sed, but I don't know if it fits my use case.

Score:0
hr flag

There are several ways to do this

  1. pass multiple patterns with -e ex.

    somecommand | grep -e foo -e bar -e baz
    
  2. use a regular expression that matches multiple patterns ex. using the extended regular expression alternation operator |

    somecommand | grep -E 'foo|bar|baz'
    
  3. put the patterns one-per-line in a file, and pass the file to grep via the -f option ex.

    somecommand | grep -f patfile 
    

    where

    $ cat patfile
    foo
    bar
    baz
    
Score:0
us flag

You can use grep for this. And there are several approaches, look here, for example:

  1. Use the escaped pipe symbol in the expression:

    <command that spits out text> | grep "text to find\|another text to find"
    
  2. Use grep with the -E option:

    <command that spits out text> | grep -E "text to find|another text to find"
    
  3. Use grep with -e options:

    <command that spits out text> | grep -e "text to find" -e "another text to find"
    
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.