0

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.

0

3 Answers 3

1

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"
    
0

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
    
0

If you'd like to grep multiline after your query string, you can use it

grep -A 3 "your query string" your_file

3 is the line number you want to see after your condition.

Line number after your query: -A NUM, --after-context=NUM

Line number before your query: -B NUM, --before-context=NUM

Remember you can always use -i option to ignore case-sensitively your_first_command | grep -i 'your query string' 'your file_name'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .