2

I'm using this command to retrieve the signal average power of a client connected to an Access Point:

iw dev wlan0 station dump | grep -E 'Station|signal avg': 

I got the following info:

Station "my_MAC_Address" (on wlan0)

signal avg: -46 dBm

In bold is what I matches with grep and I just only want to get the word after that matching, i.e the MAC address and the number -46. I've been playing with awk but without success. hope you can help me!

progloverfan
  • 155
  • 2
  • 13

3 Answers3

8
iw dev wlan0 station dump | grep -Po '(?<=Station\s|signal avg:\s)[^\s]*'

This regexp uses a so-called lookbehind syntax. You can read about it here

Example output:

00:11:22:33:44:55
-40

Update:

Thanks for voting this answer up. Now I know another solution:

iw dev wlan0 station dump | grep -Po '(Station\s|signal avg:\s)\K[^\s]*'

Which is actually a shorthand for the solution above. \K basically means "forget everything before its occurance".

Aleks-Daniel Jakimenko-A.
  • 10,335
  • 3
  • 41
  • 39
2

You can use two grep to do this as well

iw dev wlan0 station dump | grep -E 'Station|signal avg' | grep -o [^'Station|signalavg'].*
Charles Chow
  • 1,027
  • 12
  • 26
0

One possible awk solution which plays fast and loose with whitespace is:

... | awk '$1 == "Station" { print $2 } 
           $1 $2 == "signalavg:" { print $3 }' 
William Pursell
  • 204,365
  • 48
  • 270
  • 300