简体   繁体   中英

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

To get the 'Permanent' line:

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

To get the 'Current' line:

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

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.

If you want grep:

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

A single grep for each address:

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

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

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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM