From: "rob pike" <rob@plan9.bell-labs.com>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] scripting sam
Date: Fri, 22 Dec 2000 09:21:03 -0500 [thread overview]
Message-ID: <20001222142105.3A8D2199DD@mail.cse.psu.edu> (raw)
Sam prints messages to stderr, so unlike the silly '-' argument to silence
ed, you can say
sam -d foo >[2] /dev/null <<!
...
!
Here is an example of the combined power and idiocy of this approach.
The code is radically dependent on details of the HTML, which changes
about once a year, inviting me to dive back in to this enchanting world.
% cat /bin/weather
#!/bin/rc
rfork e
fn sigint sighup sigterm{
rm -f /tmp/w$pid
exit 1
}
fn sigexit{
rm -f /tmp/w$pid
exit ''
}
fn usage{
echo usage:
echo ' 'weather known-name
echo ' 'known-names: '
newark
new york
sf
san fransisco
la
los angeles'
echo ' 'weather New_York NY
echo ' 'weather Toronto CN
echo for a list of cities in new york, say
echo ' ' weather list NY
exit
}
city = Newark
state = _NJ
country = US
switch($*){
case '?'
usage
case newark local ''
city = Newark; state = _NJ; country = US
case 'new york'
city = New_York; state = _NY
case 'sf' 'san francisco'
city = San_Francisco; state = _CA
case 'la' 'los angeles'
city = Los_Angeles; state = _CA
case 'north pole'
city = North_Pole_Nwt; state = ''; country = CN
case 'list '*
city = list
state = $2
case *
if(! ~ $#* 2) usage
city = $1
state = $2
switch($state){
case AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA DC WV WI WY PR
state = _$state
country = US
case *
country = $state
state = ''
}
}
if(~ $city list){
if (! hget http://weather.yahoo.com/regional/US/^$state^.html > /tmp/w$pid) exit $status
grep 'href="forecast' /tmp/w$pid | sed -n 's/.*forecast.//
s/_'$state'_.*$//
s/.*/weather & '$state'/p'
exit
}
if (! hget http://weather.yahoo.com/forecast/^$city^$state^_^$country^_f.html > /tmp/w$pid) exit $status
#cat /tmp/w$pid
sam -d /tmp/w$pid >[2] /dev/null <<'!'
,s/°/°/g
,s/ //g
/^<title>.*\n/ {
y!</?title>!p
1,.d
}
f MAIN
B LOCAL
b MAIN
/Local Forecast/,$m "LOCAL"
b MAIN
,x/<[aA] ([^>]|\n)+>/ .,/<\/[aA]>/d
,x/<([^>]|\n)+>/d
,x/>/d
,x/Yahoo!.+\n/d
/^F°.+\n/d
/^‹10/;/^100\+\n/d
0;0/^ *-.+\n/d
0/.*[0-9]+°/,$-/^(Sunset|Barometer).+\n/m0/^Today\n/
/^Today/,$-/(^Sunset|Barometer)/ x/\n\n+/c/\n/
/^Today/;.+3 s/\n/ /g
/^Current conditions/,/^Neighboring.+\n/d
,x/\n *(\n *)+/c/\n\n/
,x/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)$/ a/:/
0/^(Haze|Snow|Windy|Rain|Showers|Sunny|((Most|Part)ly *)?Cloudy)/m0/:$/
0/^(Haze|Snow|Windy|Rain|Showers|Sunny|((Most|Part)ly *)?Cloudy)/m0/:$/
0/^(Haze|Snow|Windy|Rain|Showers|Sunny|((Most|Part)ly *)?Cloudy)/m0/:$/
0/^(Haze|Snow|Windy|Rain|Showers|Sunny|((Most|Part)ly *)?Cloudy)/m0/:$/
,x/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat):.*/ a/:/
0/^Hi .*/m0/:$/
0/^Hi .*/m0/:$/
0/^Hi .*/m0/:$/
0/^Hi .*/m0/:$/
,x/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat):.*/ a/:/
0/^Lo .*/m0/:$/
0/^Lo .*/m0/:$/
0/^Lo .*/m0/:$/
0/^Lo .*/m0/:$/
/^Copyright.+\n/d
,x/ *-/d
,x/>/d
,x/ *=/d
,x/\n *(\n *)+/c/\n\n/
0/\n+/d
,p
b LOCAL
,x/<[aA] [^>]+>/ .,/<\/[aA]>/d
,x/<([^>]|\n)+>/d
/^Copyright.+\n/d
,x/ *-/d
,x/ *=/d
,x/\n *(\n *)+/c/\n\n/
,p
!
next reply other threads:[~2000-12-22 14:21 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-12-22 14:21 rob pike [this message]
2001-01-02 17:24 ` cLIeNUX user
-- strict thread matches above, loose matches on Subject: below --
2001-01-03 0:34 Russ Cox
2000-12-23 19:34 anothy
2001-01-02 17:35 ` cLIeNUX user
[not found] <200012222249.XAA04024@unix12.planete.net>
2000-12-23 3:46 ` Boyd Roberts
2000-12-23 9:53 ` matt
2000-12-23 11:26 ` Boyd Roberts
2000-12-23 11:54 ` matt
2000-12-24 1:18 ` Boyd Roberts
2000-12-23 1:13 forsyth
[not found] <200012222214.XAA00448@unix12.planete.net>
2000-12-22 22:34 ` kilgore
2000-12-22 22:48 ` James A. Robinson
[not found] <rsc@plan9.bell-labs.com>
2000-12-22 6:37 ` Russ Cox
2000-12-23 2:51 ` Scott Schwartz
2000-12-22 6:33 anothy
2000-12-22 14:14 ` Mark C. Otto
2000-12-22 22:07 ` Boyd Roberts
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20001222142105.3A8D2199DD@mail.cse.psu.edu \
--to=rob@plan9.bell-labs.com \
--cc=9fans@cse.psu.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).