View previous topic :: View next topic |
Author |
Message |
defer- Tux's lil' helper
Joined: 11 Jun 2007 Posts: 140 Location: Finland
|
Posted: Tue Jul 23, 2013 10:57 am Post subject: Looking for bash/sed/awk tutorial |
|
|
Im looking for good bash/sed/awk tutorial. Is there any better resources than tldp.org documentation or man pages?
Im trying to parse iw wlan0 scan output like this :
Code: | SSID channel signal encryption
wlan-ap 6 70% wpa2-psk
test 1 55% wep |
_________________ https://github.com/defer-
Last edited by defer- on Tue Jul 23, 2013 12:42 pm; edited 1 time in total |
|
Back to top |
|
|
ppurka Advocate
Joined: 26 Dec 2004 Posts: 3256
|
|
Back to top |
|
|
yngwin Retired Dev
Joined: 19 Dec 2002 Posts: 4572 Location: Suzhou, China
|
|
Back to top |
|
|
defer- Tux's lil' helper
Joined: 11 Jun 2007 Posts: 140 Location: Finland
|
|
Back to top |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10587 Location: Somewhere over Atlanta, Georgia
|
Posted: Sun Jul 28, 2013 1:25 pm Post subject: |
|
|
For AWK, in my opinion, nothing beats the original Aho, Weinberger, Kernigan book. Well worth the bucks for the dead tree. There's also a pretty good book by Arnold Robbins that covers sed and AWK. Don't have any specific recommendations for Bash as I kind of learned it by osmosis.
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
truc Advocate
Joined: 25 Jul 2005 Posts: 3199
|
Posted: Sun Jul 28, 2013 2:48 pm Post subject: |
|
|
Doing what you want with sed is just asking for trouble, but I did something like this before (just for the sake of doing it with sed) and I kept the result, so here is a live example of some sed script:
Code: | $ declare -f myiwscan
myiwscan ()
{
sudo iw dev "$1" scan | sed -n '/^BSS/ {
: start
h
# continue with next lines
: loop
n
: cleanme
s/^[[:space:]]*//
# hold some lines
/^freq:/b holdme
/^signal:/b holdme
/^SSID:/b holdme
/^DS / {s/.*: //; b holdme }
# just in case
/^BSS[[:space:]]/b printme
b loop
: holdme
# hold the line
H
/^channel[[:space:]]\+[0-9]\+$/b printme
b loop
:printme
x
# move the channel around
s/^\(.*\nfreq:[[:space:]]\+[0-9]\+\)\n\(.*\)channel[[:space:]]\([0-9]\+\)$/\1 chan: \3 \2/
s/\n/ /g
p
# and just in case
x
/^BSS[[:space:]]/b start
}'
}
|
which you can use like this:
Code: | $ myiwscan wlp3s0 | head
BSS 30:7e:cb:84:3f:44(on wlp3s0) -- associated freq: 2412 chan: 1 signal: -31.00 dBm SSID: SFR_3F40
BSS a2:7e:cb:84:3f:45(on wlp3s0) freq: 2412 chan: 1 signal: -81.00 dBm SSID: SFR WiFi FON
BSS a2:7e:cb:84:3f:47(on wlp3s0) freq: 2412 chan: 1 signal: -38.00 dBm SSID: SFR WiFi Mobile
BSS e0:a1:d7:f1:cc:94(on wlp3s0) freq: 2412 chan: 1 signal: -68.00 dBm SSID: SFR_CC90
BSS ca:a1:d7:f1:cc:95(on wlp3s0) freq: 2412 chan: 1 signal: -71.00 dBm SSID: SFR WiFi FON
BSS ca:a1:d7:f1:cc:97(on wlp3s0) freq: 2412 chan: 1 signal: -68.00 dBm SSID: SFR WiFi Mobile
BSS 5c:33:8e:d0:bf:b3(on wlp3s0) freq: 2437 chan: 6 signal: -77.00 dBm SSID: Livebox-7d24
BSS e0:a1:d7:e7:ff:64(on wlp3s0) freq: 2437 chan: 6 signal: -82.00 dBm SSID: SFR_FF60
BSS b2:a1:d7:e7:ff:67(on wlp3s0) freq: 2437 chan: 6 signal: -82.00 dBm SSID: SFR WiFi Mobile
BSS 00:25:15:ef:a0:ac(on wlp3s0) freq: 2442 chan: 7 signal: -78.00 dBm SSID: SFR_A0A8
|
This script is probably just a good introduction to what you get when you don't choose the right tool from the beginning! It was an interesting exercice though _________________ The End of the Internet! |
|
Back to top |
|
|
defer- Tux's lil' helper
Joined: 11 Jun 2007 Posts: 140 Location: Finland
|
Posted: Sun Jul 28, 2013 6:23 pm Post subject: |
|
|
I already solved it with awk like this:
Code: | /^BSS / {
MAC = $2
wifi[MAC]["enc"] = "Open"
}
$1 == "SSID:" {
wifi[MAC]["SSID"] = $2
}
$1 == "freq:" {
wifi[MAC]["freq"] = $2
}
$1 == "signal:" {
wifi[MAC]["sig"] = $2 " " $3
}
$1 == "capability:" {
wifi[MAC]["type"] = $2
}
$1 == "WPA:" {
wifi[MAC]["enc"] = "WPA"
}
$1 == "WEP:" {
wifi[MAC]["enc"] = "WEP"
}
END {
for (w in wifi) {
if ( i++ > 0 ) { printf "\n" }
printf "SSID: %s\nType: %s\nFrequency: %s\nSignal: %s\nEncryption: %s\n"\
,wifi[w]["SSID"],wifi[w]["type"],wifi[w]["freq"],wifi[w]["sig"],wifi[w]["enc"]
}
}' |
Output:
Code: | ~ $ cwm -s
SSID: netti
Type: ESS
Frequency: 2437
Signal: -33.00 dBm
Encryption: Open
SSID: net
Type: IBSS
Frequency: 2457
Signal: -37.00 dBm
Encryption: Open
SSID: WLAN-AP
Type: ESS
Frequency: 2462
Signal: -81.00 dBm
Encryption: WPA |
_________________ https://github.com/defer- |
|
Back to top |
|
|
|