Register
It is currently Thu Dec 14, 2017 6:58 am

Find text in text


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Tue Feb 16, 2016 10:38 pm   

Joined: Tue Feb 16, 2016 10:28 pm
Posts: 2
I have a bash script (.sh file) that puts weather conditions on an image. I am having problems getting the -11 near the end of the string. The text file that I am getting the info from is attached.

The -11 is this one in the file

Code:
<dt>
                  <a href="http://ec.gc.ca/meteo-weather/default.asp?lang=En&amp;n=5FBF816A-1" title="Wind Chill">Wind Chill</a>:
            </dt>
                <dd class="mrgn-bttm-0 wxo-metric-hide">-11</dd>
                <dd class="mrgn-bttm-0 wxo-imperial-hide wxo-city-hidden">12</dd>
              </dl>


I tried the following but it returns -11 -11.

windchill=$(sed -nr '/Wind Chill/{n;n;s/.*>(.*)<.*/\1/p}' "$weatherFile")

How do I just find the first -11 ??


EDIT: It will not let me upload the file.


Top
 Profile  
 PostPosted: Tue Feb 16, 2016 11:32 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 273
Hello!

It looks like your sed statement is doing exactly what you want. When I go to your source page (I followed the URL in your example text, and picked a random page from there), the pattern you're looking for occurs twice, so sed finds and prints it twice!

The way I see it, you have a couple of options...

Option 1: Have sed quit after the first match
Code:
## Just add the 'q' command to make
## sed quit after it prints the first match
windchill=$(sed -nr '/Wind Chill/{n;n;s/.*>(.*)<.*/\1/p;q}' "$weatherFile")


Option 2: Use their RSS feed! You *might* find the xml version a little easier to parse than the html.
Code:
data=$(wget -qO- http://www.weather.gc.ca/rss/city/ab-52_e.xml)
windchill=$(echo "$data" |sed -nr '/Wind Chill:/{s/.*>\s(.*)\s<.*/\1/p;q}')
echo "Wind Chill: $windchill"


I hope this helps!


Top
 Profile YIM  
 PostPosted: Wed Feb 17, 2016 6:39 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
Do not parse XML with sed, grep or awk.
Tedious and cumbersome.

Use xml_grep or xmlstarlet


Top
 Profile  
 PostPosted: Wed Feb 17, 2016 6:50 am   

Joined: Tue Feb 16, 2016 10:28 pm
Posts: 2
Thanks for the replies. The q fixed the problem, but I am going to look into the xml file as well.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP