On Wed, Jul 16, 2025 at 11:59 PM Ben Koenig <techkoe...@protonmail.com>
wrote:

>
>
>
>
> -Ben
>
>
> On Wednesday, July 16th, 2025 at 11:05 PM, Ben Koenig <
> techkoe...@protonmail.com> wrote:
>
> > On Wednesday, July 16th, 2025 at 9:57 PM, Michael Barnes
> barnmich...@gmail.com wrote:
> >
> > > I have a script that is supposed to grab a weather feed and return the
> > > current temperature. If grabs the file fine, but does not return the
> > > temperature.
> > >
> > > Here is the line that fails:
> > >
> > > T=$(grep "<temp_f>" KEUG.xml |cut -c10- |cut -d '.' -f 1)
> > >
> > > Here is the KEUG.xml file:
> > > <?xml version="1.0" encoding="ISO-8859-1"?>
> > >
> > > <?xml-stylesheet href="latest_ob.xsl" type="text/xsl"?>
> > >
> > > <current_observation version="1.0" xmlns:xsd="
> > > http://www.w3.org/2001/XMLSchema"; xmlns:xsi="
> > > http://www.w3.org/2001/XMLSchema-instance";
> xsi:noNamespaceSchemaLocation="
> > > http://www.weather.gov/view/current_observation.xsd";><credit>NOAA's
> > >
> > > National Weather Service</credit><credit_URL>https://weather.gov/
> > >
> > > </credit_URL><image><url>
> http://forecast.weather.gov/images/xml_badge.png</url><title>NOAA's
> > >
> > > National Weather
> > > Service</title><link>https://www.weather.gov
> </link></image><suggested_pickup>15
> > >
> > > minutes after the
> > >
> hour</suggested_pickup><suggested_pickup_period>60</suggested_pickup_period><location>Eugene,
> > >
> > > Mahlon Sweet Field,
> > >
> OR</location><station_id>KEUG</station_id><latitude>44.13333</latitude><longitude>-123.21444</longitude><observation_time>Last
> > >
> > > Update on Jul 16 2025, 7:54 pm
> > > PDT</observation_time><observation_time_rfc822>Wed, 16 Jul 2025
> 19:54:00
> > >
> > >
> -0700</observation_time_rfc822><weather>Fair</weather><temperature_string>88.0
> > >
> > > F (31.1
> > >
> C)</temperature_string><temp_f>88.0</temp_f><temp_c>31.1</temp_c><relative_humidity>26</relative_humidity><wind_string>West
> > >
> > > at 8.1 MPH (7
> > >
> KT)</wind_string><wind_dir>West</wind_dir><wind_degrees>250</wind_degrees><wind_mph>8.1</wind_mph><wind_kt>7</wind_kt><pressure_string>1008
> > >
> > >
> mb</pressure_string><pressure_mb>1008</pressure_mb><pressure_in>29.78</pressure_in><dewpoint_string>48.9
> > >
> > > F (9.4
> > >
> C)</dewpoint_string><dewpoint_f>48.9</dewpoint_f><dewpoint_c>9.4</dewpoint_c><heat_index_string>85
> > >
> > > F (30
> > >
> C)</heat_index_string><heat_index_f>85</heat_index_f><heat_index_c>30</heat_index_c><visibility_mi>10.00</visibility_mi><icon_url_base>
> > >
> > > http://forecast.weather.gov/images/wtf/small/
> > > </icon_url_base><two_day_history_url>
> > >
> > > http://forecast.weather.gov/data/obhistory/KEUG.html
> > > </two_day_history_url><icon_url_name>nskc.png</icon_url_name><ob_url>
> > >
> > > http://forecast.weather.gov/data/METAR/KEUG.1.txt
> </ob_url><disclaimer_url>
> > >
> > > https://www.weather.gov/disclaimer.html
> </disclaimer_url><copyright_url>
> > >
> > > https://www.weather.gov/disclaimer.html
> </copyright_url><privacy_policy_url>
> > >
> > > https://www.weather.gov/notice.html
> > > </privacy_policy_url></current_observation>
> > >
> > > I should get:
> > >
> > > $ echo $T
> > > 88
> > >
> > > Instead, I get:
> > > $ echo $T
> > > observation version="1
> > >
> > > This has been working for several years. The script runs each hour,
> pulling
> > > the weather statement and culling the current temperature. The
> remainder of
> > > the script uses the variable to select the appropriate audio file to
> > > announce the current temperature. I set this up about five years ago
> and my
> > > feeble memory has no idea how it really works any more.
> > >
> > > Any ideas on what went wrong where?
> > >
> > > Thanks,
> > > Michael
> >
> >
> >
> > I think the linebreaks are missing in your datafile. you are using grep
> to search for a specific line and then cut from there, but if newlines are
> removed from the xml then it does what you described.
> >
> > XML does not require newlines in order to be valid and it's possible
> that the source of your data no longer prints the data with them.
> >
> > You will either need to make sure the downloaded xml includes all the
> expected new lines or adjust the script to make sure it doesn't matter.
> >
> > -Ben
>
> This might be a more robust one-liner to get the information you need:
>
> T=$(awk -F'temp_f>|</temp_f' '{print $2}' KEUG.xml | cut -d. -f1)
>


This seems to do the trick. Thank you so much.

Michael

Reply via email to