On Thu, Jun 13, 2013 at 04:06:58AM +0800, Vikram Verma wrote:
> On 2013-06-13, Mats Engstrom wrote:
> > Oooohhh. Krake.io sure is funky! It can definitely come in handy. Usually
> > I whip up a bash script with a lot of sed & regex -magic in it to do things
> > like this, but krake will make to so much easier.
>
> Like this?
>
> #!/usr/bin/env bash
>
> db="$HOME/postcodes.asv"
> for node in `seq 1 124289`; do
> XML=`curl -s "
http://sgp.postcodebase.com/node/${node}"`
> address() { echo -e "$XML" | grep "itemprop='description'" | awk -F '<|>' '{ print $3 }'; }
> postcode() { echo -e "$XML" | grep "itemprop='postalCode'" | awk -F '<|>' '{ print $7 }'; }
> (address; postcode) | tr '\n' $'\031' >> "$db"
> echo -n $'\030' >> "$db"
> done
> P.S. Could we move `grep`'s pattern to the `awk` expression? We'd have
> to move the separator definition to the action.. Tamas?
(not tamas but), all you need to do is move the grep search expression
into /.../ insode the awk code before the block. it is applied
independent of the seperator:
address() { echo -e "$XML" | awk -F '<|>' '/itemprop=\'description\'/ { print $3 }'; }
postcode() { echo -e "$XML" | awk -F '<|>' '/itemprop=\'postalCode\'/ { print $7 }'; }
if this doesn't work then there is an issue with the \', haven't
used awk on lines containing ' myself.
oh, since you are parsing the same $XML here and then just run those one
after another you could do it in one command:
echo -e "$XML" | awk -F '<|>' '/itemprop=\'description\'/ { print $3 }
/itemprop=\'postalCode\'/ { print $7 }';
this asumes that the description always comes before the postalCode.
if that's not the case you could save the values found in a variable: {
description=$3 and postalCode=$7 and then use END { print description
postalCode }
and even more fancy: replace print with i believe printf (need to look
that one up) to allow you to output the values without a newline so you
can drop the tr.
you can then also pipe the curl output directly though awk:
curl -s "
http://sgp.postcodebase.com/node/${node} | awk ... >> $db
greetings, martin.
--
eKita - the online platform for your entire academic life
hackerspace beijing -
http://qike.info
--
chief engineer eKita.co
pike programmer
pike.lysator.liu.se caudium.net
foresight developer
realss.com foresightlinux.org
unix sysadmin trainer developer
societyserver.org
Martin B�hr working in china
http://societyserver.org/mbaehr/