Glam Prestige Journal

Bright entertainment trends with youth appeal.

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

2 Answers

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"

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy