Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Note that there are two scripts here, one sed (GNU sed), one awk (gawk).
- Both are invoked through a bash function which finds your local/preferred forecast page using DuckDuckGo's
- bang search, such as:
- noaa ()
- {
- w3m -B -dump "https://duckduckgo.com/lite?q=\!noaa ${1:-20755}" | ~/bin/weather.sed | cat -s | ~/bin/weather.awk | less
- }
- --------------------------------------------------------------------------------
- #!/bin/sed -nEf
- # NB: This relies on GNU sed, BSD sed (MacOS lacks features used, so use gsed from Homebrew on your Mac.
- # General cleanup...
- 1,/Your local forecast office is/d
- # 1,/\[news-impor\]/d # Commented Tue 28 May 2019 11:36:32 PM CDT
- 1,/^./ {
- /^$/d
- /^./ b print
- }
- # Delete blocks
- /^Zone Area Forecast/,/^Point Forecast/ {
- /^Point Forecast/ { n; n; b end }
- /./ d
- }
- # Delete to end.
- # /^Additional Forecasts and Information/,$d
- /^Forecast Discussion/,$d
- # Linefeed before date in extended forecast
- /^Detailed Forecast/,/^Additional Forecasts and Information/ {
- /^Additional Forecasts and Information/ {b end}
- /^Detailed Forecast/ { i \
- b print
- }
- /^(This|Today|Tonight|Overnight|Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday).*/ { i \
- b print
- }
- /^./ {
- s/^./ &/
- b print
- }
- }
- # ab
- /^Current conditions at/ { h; n; n; H; x; s/\n/ /; p; b end }
- /^Lat/,+8 {
- # /^Humidity/ b humidity
- /^Lat/ { s/(Lon|Elev)/ &/g; p; b; }
- /[][]/d;
- /^[^0-9].*[^CF]*$/ { h; n; n; H; n; n; H; x; s/\n/ /g; b print }
- }
- : humidity
- /^Humidity/,/^$/ {
- /^./ { s/^./ &/; b print }
- /^%/ b print
- }
- /^Extended Forecast for/ { h; n; n; H; x; s/\n/ /g; b print }
- /^(Last Update|Forecast Valid):/ { h; n; H; x; s/\n/ /g; b print }
- # Fix broken bullets
- /^ *. *$/ { h; n; H; x; s/\n/ /g; s/^( *.) {1,}/\1 /; b print }
- /NOW:/ { h; n; H; x; s/\n/ /; p; b }
- : print
- # Capture any stuff we ought to delete.
- /En Espa.ol/d
- /Share/d
- /Click HERE/d
- /^Click here/d
- /^More Information:/d
- /^Local Forecast Office/d
- /[][]/d
- /^Forecast$/d
- s/Read More >//
- s/Similar City Names//
- p
- : end
- --------------------------------------------------------------------------------
- #!/usr/bin/gawk -f
- ## There are likely gawkisms here, One True Awk fans beware
- # Print a horizontal bar
- function hbar (width) {
- for ( i=0; i<width; i++ ) printf( "_" )
- printf("\n")
- }
- BEGIN {
- mode = "start"
- col = 80
- bullet_i = -1
- printf( "Weather Forecast retrieved %s.\n", strftime( "%A %B %e, %Y %H:%M:%S", systime() ))
- hbar(col)
- printf("\n")
- }
- {
- bullet = 0
- newmode = 0
- oldrecord = ""
- newrecord = ""
- mergeline = 0;
- }
- # Detect bullets
- /^ *[^ A-Za-z0-9] / {
- bullet = 1
- }
- /^Hazardous Weather Conditions/ { mode = "hazard"; newmode = 1 }
- /^Current conditions/ { mode = "current"; newmode = 1 }
- /^Extended Forecast/ { mode = "extended"; newmode = 1 }
- /^Detailed Forecast/ { mode = "detailed"; newmode = 1 }
- /^Last Update/ { mode = "update"; newmode = 1 }
- {
- if ( mode == "hazard" ) next
- # printf( "%-12s ", mode )
- if ( mode == "update" && newmode == 1 ) {
- printf( "\n" )
- hbar(col)
- printf( "%s\n", $0 )
- next
- }
- if ( mode != "extended" ) bullet_i = -1
- if ( newmode == 1 ) {
- printf( "\n\n" )
- print $0
- hbar(col)
- next
- }
- # Collapse short lines in extended mode...
- # ... bullets
- if ( bullet == 1 && mode == "extended" ) {
- # Bullet lines themselves
- bullet_i = 0
- oldline = gensub( "\\s*$", "", "g", $0 )
- getline
- newline = gensub( "^\\s*", "", "g", $0 )
- mergedline = gensub( "\\n", " ", "g", oldline " " newline )
- if ( $0 !~ /^$/ ) printf ( "%s\n", mergedline )
- else printf( "%s\n\n", oldline )
- next
- }
- if ( mode == "extended" && bullet_i >= 0 && $0 !~ /^Detailed Forecast/ ) {
- bullet_i++
- # printf( " extended bullet " )
- if ( $0 ~ /^$/ && bullet_i > 1 && length(oldline) > 1 ) {
- # printf( "shooting blanks: %s %s %s\n", bullet_i, length(oldline), $0 );
- printf( "%s\n", $0 );
- oldline = $0
- next
- }
- if ( $0 !~ /^$/ ) {
- oldline = gensub( "\\s*$", "", "g", $0 )
- if ( length( oldline ) > 40 ) {
- printf( "%s\n", oldline ); next
- }
- while ( getline ) {
- # Do not overflow into detailed forecast ...
- if ( $0 ~ /^Detailed Forecast/ ) {
- mode = "detailed"
- bullet_i = -1
- newmode = 1
- printf( "\n\n%s\n", $0 )
- hbar(col)
- next
- }
- if ( length($0) == 0 ) {
- printf( "%s\n\n", oldline )
- next
- }
- else if ( length($0) > 0 ) {
- newline = gensub( "^\\s*", "", "g", $0 )
- mergedline = gensub( "\\n", " ", "g", oldline " " newline )
- oldline = mergedline
- continue
- }
- newline = gensub( "^\\s*", "", "g", $0 )
- if ( length (oldline) < 40 && length(newline) > 0 ) {
- mergedline = gensub( "\\n", " ", "g", oldline " " newline )
- printf( "%s\n", mergedline )
- oldline = $0
- next
- }
- else {
- printf( "%s\n", oldline )
- printf( "%s\n", $0 )
- oldline = $0
- next
- }
- }
- }
- }
- }
- {printf( "%s\n", $0) }
Advertisement
Add Comment
Please, Sign In to add comment