sidney
sidney

Reputation: 2714

Returns a part of a result (GREP)

With this command, I would like one command to get the current mac adress only, and another command to get only the permanent mac. So I have to use a grep expression, but I don't know what to do.

    $ macchanger -s wlan0
    Permanent MAC: 14:25:47:ff:c4:aa (Twinhan)
    Current MAC: 00:24:54:f0:5c:cc (unknown)

So I would really like to do something like macchanger -s wlan0 | grep ... in order to exactly get 14:25:47:ff:c4:aa And another command to get 00:24:54:f0:5c:cc Thanks you

Upvotes: 0

Views: 175

Answers (6)

Barmar
Barmar

Reputation: 780688

This solution depends on the order of the results in the output:

declare -a results
results=($(macchangher -s wlan0 | egrep -oi '([a-f0-9]{2}:){5}[a-f0-9]{2}'))
perm=$results[0]
cur=$results[1]

Upvotes: 0

Martin Ender
Martin Ender

Reputation: 44259

A single grep for each address:

grep -P -o '(?<=Permanent MAC: )[a-zA-Z0-9:]+'
grep -P -o '(?<=Current MAC: )[a-zA-Z0-9:]+'

Upvotes: 1

Bitwise
Bitwise

Reputation: 7807

If you want grep:

macchanger -s wlan0 | grep Permanent | grep -P -o '..:..:..:..:..:..'
macchanger -s wlan0 | grep Current | grep -P -o '..:..:..:..:..:..'

Upvotes: 1

Jamey Sharp
Jamey Sharp

Reputation: 8491

The -o option to grep will do what you want. For example:

grep -o '[0-9a-f:]\{17\}'

Upvotes: 0

Steve
Steve

Reputation: 54392

To get the 'Permanent' line:

macchanger -s wlan0 | awk '/Permanent/ { print $3 }'

To get the 'Current' line:

macchanger -s wlan0 | awk '/Current/ { print $3 }'

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 753475

I think you would be better off using sed than grep:

macchanger -s wlan0 | sed -n '/^Permanent/s/Permanent MAC: \([0-9a-fA-F:]*\) .*/\1/p'
macchanger -s wlan0 | sed -n '/^Current/s/Current MAC: \([0-9a-fA-F:]*\) .*/\1/p'

This would work with a POSIX-compliant sed; GNU sed sometimes has a mind of its own, but it accepts these and works as expected.

Upvotes: 1

Related Questions