2011-04-17

Ham Lookup Bash Script (fragile)

Earlier today I wanted to quickly find some info on some Hams, but I couldn't remember the FCC link. Then I found the QRZ site. Not bad, but I didn't want to have to launch Firefox just to look someone up, so I thought maybe I'd write a python script. Except then I couldn't think of an easy Regex to parse the HTML So I fetched the page source, found the form that looks up names and callsigns, pulled down my own info, and before I knew it, I had a series of ugly greps and seds pulling out the relevant information. Then something strange happened. I lost interest in pursuing the python angle. I dumped my two queries into two separate bash scripts named lookupname and lookupsign. It was quick and painless, even though the "code" is horribly fragile and entirely dependant on the HTML coming back, but it (currently) works so I'm calling it good for now.

A few minutes ago I was sitting on a different computer and wanted to lookup a callsign, again, so I dumped the two separate bash scripts into a single script and I guess I'll share that in case I want it again in the future. Here's the script:

 1 #!/bin/bash
 2
 3 SIGN="`echo $@ | sed 's/\ /+/g'`"
 4
 5 echo "$SIGN" | grep +
 6
 7 if [ "$?" -eq 0 ] ; then
 8         echo "Looking for name $SIGN"
 9         wget --post-data="callsign=$SIGN" http://www.qrz.com/db/ -O - 2>/dev/null | egrep -o '<td class="(rc|ra)">.*</td>' | egrep -o '>(([A-Z]|[a-z]|[0-9])|([A-Z]|[a-z]|[., ]))*<' | sed 's/<//g' | sed 's/>//g' | grep -v '^$'
10 else
11         echo "Looking for callsign $SIGN"
12         wget --post-data="callsign=$1" http://www.qrz.com/db/ -O - 2>/dev/null | egrep '<p>.*</p>' | sed 's/<b.*">//g' | sed 's/<\/b>//g' | sed 's/<p>//g' | sed 's/<\/p>//g'
13
14 fi



Here's the same script. Invoke it with either a callsign or a person's name. Basically, if you want to do a callsign lookup make sure there are no spaces in the invokation, ex: lookupham AB2CDE. If you want to do a name lookup then make sure you provide a first and last or if you're clever make sure there is a space in the parameter list. ex: lookupham John Doe.

No comments: