Jump to content

Using grep with a variable?


Recommended Posts

I was working on a script that would get MAC addresses from a text file then indentify their make. I ran into  a problem with grep while trying to use a variable for pattern matching.

Firstly I was getting the MACs from a file and save them to a separate file. #I suck at regular expressions so you know a shorter way to write this please tell me.

grep -io '[A-Z0-9]\{2\}:[A-Z0-9]\{2\}:[A-Z0-9]\{2\}:[A-Z0-9]\{2\}:[A-Z0-9]\{2\}:[A-Z0-9]\{2\}' /root/air/NPC-01.csv | sort -u > /root/air/macs.txt

The following statement works if I want to retrieve a single manufacturer.

grep $(echo 00:20:8C:30:40:60 | cut -d ':' -f 1,2,3 | sed 's/:/-/g') /etc/unicornscan/oui.txt | cut -d ':' -f 2

But really I want to to something like this. The problem is I'm not sure if grep will even work like this. Basically I want to take $line from mac.txt (where macs.txt is simply a list of MAC addresses) and get the first three hexadecimal pairs and check them against oui.txt

cat /root/air/macs.txt | while read line; do

grep $(echo $line | cut -d ':' -f 1,2,3 | sed 's/:/-/g') /etc/unicornscan/oui.txt | cut -d ':' -f 2;  done

I tried this a couple of different ways. I wasn't sure how to make grep or egrep take variables. Basically the problem I've been having is grep will want to puke out the entire contents of oui.txt or nothing at all.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...