FoobarBazbot

weather.awk

Apr 12th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 0.86 KB | None | 0 0
  1. #!/usr/bin/gawk -f
  2. {
  3.   location=$0;
  4.   gsub(/ /,"+",location);
  5.   pipe="curl -s http://weather.gladstonefamily.net/site/search?site=" location;
  6.   RSsave=RS;
  7.   RS="<[lL][iI]>";
  8.   while(pipe |getline) {
  9.     if($0 ~ /href="\/site\/[^"]/ && $0 !~ /\[no data\]/) {
  10.       site=gensub(/.*href="\/site\/([^"]+).*/,"\\1", "g");
  11.       station=gensub(/.*href="[^\"]+">([^<]+).*/,"\\1", "g");
  12.       close(pipe);
  13.       pipe="curl -s http://weather.gladstonefamily.net/cgi-bin/wxobservations.pl?site=" site "&days=1";
  14.       RS="\n";
  15.       FS=",";
  16.       while(pipe |getline) { } #discard all but last data line
  17.       CONVFMT="%.1f";
  18.       print "weather for " station ": " $2 " mbar, " $3 "F/" ($3-32)/1.8 "C/" ($3+459.67)/1.8 "K";
  19.       print "wind " $6 "mph / " $6*1.60934 "kph / " $6*0.44704 "m/s, " $7 " degrees";
  20.       break;
  21.     }
  22.   }
  23.   close(pipe);
  24.   RS=RSsave;
  25. }
Add Comment
Please, Sign In to add comment