Show current weather for any US city or zipcode

weather() { lynx -dump "$*" | sed 's/^ *//;/ror has occ/q;2h;/__/!{x;s/\n.*//;x;H;d};x;s/\n/ -- /;q';}
Scrape the National Weather Service
Sample Output
$ weather chicago, il
Chicago, IL -- Mostly Cloudy: 47 ?F
$ weather los angeles
Los Angeles, CA -- Fair: 83 ?F
$ weather 02456
New Town, MA -- Fair: 44 ?F

By: zude
2009-10-17 23:47:47

These Might Interest You

  • grab the weather, with a little expletive fun. replace the 48104 with a US zipcode, or the name of your city (such as ZIP="oslo"), unless you want to know what the weather is like for me (and that's fine too) Show Sample Output

    ZIP=48104; curl$ZIP 2>/dev/null|grep -A1 'div class="large"'|tr '\n' ' '|sed 's/^.*"large" >\(..\)/\1/;s/&d.* <br \/>/ - /;s/<br \/>//;s/<\/div.*$//'
    sleepynate · 2009-08-29 19:33:35 3
  • Weather based on your location

    curl -s$(curl -s "$(curl -s" | grep City | sed 's/City: \(.*\)/\1/' | sed 's/ /%20/g' | sed "s/'/%27/g") | sed 's|.*<temp_f data="\([^"]*\)"/>.*|\1\n|'
    matthewbauer · 2010-02-13 21:42:48 8
  • Uses Google's "OneBox" to look up the sunrise in any city by name. If no city is specified, it defaults to Seattle. For the sunset time, you change the search query to "sunset", like so, . sunset() { city=${1-Seattle}; w3m "$city" | sed -r '1,/^\s*1\./d; /^\s*2\./,$d; /^$/d' ;} . "OneBox" is Google's term for that box that appears before the organic search results that has useful information that Google thinks you might be looking for (mathematical calculations, weather, currency conversions, and such). I'm not actually using OneBox correctly, but that's because I'm not sure that there is a "correctly". I looked for a command line API, but couldn't find one, so I settled on parsing stdout from the fantastic w3m web browser. I use the sed script to show only the first hit by deleting everything from the beginning of the file until it sees " 1." and then deleting everything from " 2." to the end of the file. Ugly and fragile, yes, but it works fine. . BUG1: w3m represents the picture of the sun rising, "weather_sunset-40.gif" as "[weat]" which is slightly confusing and probably should be removed. . BUG2: The output is more easily readable by a human, which means it's less useful for scripting. Show Sample Output

    sunrise() { city=${1-Seattle}; w3m "$city" | sed -r '1,/^\s*1\./d; /^\s*2\./,$d; /^$/d' ;}
    hackerb9 · 2010-11-02 21:24:23 1
  • This shell function grabs the weather forecast for the next 24 to 48 hours from Replace <YOURZIPORLOCATION> with your zip code or your "city, state" or "city, country", then calling the function without any arguments returns the weather for that location. Calling the function with a zip code or place name as an argument returns the weather for that location instead of your default. To add a bit of color formatting to the output, use the following instead: weather(){ curl -s "${@:-<YOURZIPORLOCATION>}"|perl -ne '/<title>([^<]+)/&&printf "\x1B[0;34m%s\x1B[0m: ",$1;/<fcttext>([^<]+)/&&print $1,"\n"';} Requires: perl, curl Show Sample Output

    weather(){ curl -s "${@:-<YOURZIPORLOCATION>}"|perl -ne '/<title>([^<]+)/&&printf "%s: ",$1;/<fcttext>([^<]+)/&&print $1,"\n"';}
    eightmillion · 2010-02-10 01:23:39 8

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: