Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Looking for bash/sed/awk tutorial
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
defer-
Tux's lil' helper
Tux's lil' helper


Joined: 11 Jun 2007
Posts: 140
Location: Finland

PostPosted: Tue Jul 23, 2013 10:57 am    Post subject: Looking for bash/sed/awk tutorial Reply with quote

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
View user's profile Send private message
ppurka
Advocate
Advocate


Joined: 26 Dec 2004
Posts: 3256

PostPosted: Tue Jul 23, 2013 12:10 pm    Post subject: Reply with quote

Here are some links:

http://www.ibm.com/developerworks/linux/library/l-bash/index.html

http://www.ibm.com/developerworks/library/l-sed1/index.html

http://www.catonmat.net/blog/wp-content/uploads/2008/09/sed1line.txt
_________________
emerge --quiet redefined | E17 vids: I, II | Now using kde5 | e is unstable :-/
Back to top
View user's profile Send private message
yngwin
Retired Dev
Retired Dev


Joined: 19 Dec 2002
Posts: 4572
Location: Suzhou, China

PostPosted: Sat Jul 27, 2013 9:23 pm    Post subject: Reply with quote

For bash I always recommend http://mywiki.wooledge.org/BashGuide and http://mywiki.wooledge.org/BashFAQ
I learnt a lot from there.
_________________
"Those who deny freedom to others deserve it not for themselves." - Abraham Lincoln
Free Culture | Defective by Design | EFF
Back to top
View user's profile Send private message
defer-
Tux's lil' helper
Tux's lil' helper


Joined: 11 Jun 2007
Posts: 140
Location: Finland

PostPosted: Sun Jul 28, 2013 12:58 pm    Post subject: Reply with quote

http://www.gnu.org/software/bash/manual/bashref.html
_________________
https://github.com/defer-
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10587
Location: Somewhere over Atlanta, Georgia

PostPosted: Sun Jul 28, 2013 1:25 pm    Post subject: Reply with quote

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
View user's profile Send private message
truc
Advocate
Advocate


Joined: 25 Jul 2005
Posts: 3199

PostPosted: Sun Jul 28, 2013 2:48 pm    Post subject: Reply with quote

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 :lol:
_________________
The End of the Internet!
Back to top
View user's profile Send private message
defer-
Tux's lil' helper
Tux's lil' helper


Joined: 11 Jun 2007
Posts: 140
Location: Finland

PostPosted: Sun Jul 28, 2013 6:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum