LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-16-2014, 04:48 PM   #1
lapishater276
Member
 
Registered: Jun 2014
Location: Narnia
Distribution: Crunchbang, Arch Linux ARM, Xubuntu
Posts: 43

Rep: Reputation: 0
using grep to find text sections


i want to use the grep tool to be able to find a city in an xml document by locating the tag after the state and the for before the town. here is part of the code. this code is located in ~/.cache/weather.xml
<description>Yahoo! Weather for Town, State</description>

how would i be able to find the code?
 
Old 07-16-2014, 04:50 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,772
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
Should be simple. Can you provide a bit more of the XML that is related to what you requested?
 
Old 07-16-2014, 06:02 PM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,609

Rep: Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702
Perl would be better:

Code:
perl -ne '/Weather for (.*)\</ && print $1,"\n";' < file
 
Old 07-17-2014, 01:46 AM   #4
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 273Reputation: 273Reputation: 273
You can use sed. (just a fast example, the syntax can be improved)

if you want to extract just "Town"
Code:
echo "<description>Yahoo! Weather for Town, State</description>" | sed -n 's/.*for \(.*\),.*/\1/p'
if you want to extract "Town, State"
Code:
echo "<description>Yahoo! Weather for Town, State</description>" | sed -n 's/.*for \(.*\)<.*/\1/p'
 
1 members found this post helpful.
Old 07-17-2014, 07:44 AM   #5
lapishater276
Member
 
Registered: Jun 2014
Location: Narnia
Distribution: Crunchbang, Arch Linux ARM, Xubuntu
Posts: 43

Original Poster
Rep: Reputation: 0
how would i use sed (or perl) to look into the xml document?

lines 1-22 of xml document
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>

<title>Yahoo! Weather - Town, State</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Town__State/*http://weather.yahoo.com/forecast/USAII000_A.html</link>
<description>Yahoo! Weather for Town, State</description>
<language>en-us</language>
<lastBuildDate>Thu, 17 Jul 2014 6:14 am CDT</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Town" region="State" country="United States"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="52" direction="0" speed="0" />
<yweather:atmosphere humidity="94" visibility="10" pressure="30.08" rising="0" />
<yweather:astronomy sunrise="5:53 am" sunset="8:44 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url>
</image>

Last edited by lapishater276; 07-17-2014 at 07:50 AM.
 
Old 07-17-2014, 08:17 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
Code:
perl -ne '/for (.*),.*<\/description>/ and print "$1\n"' ~/.cache/weather.xml
- matches "the tag after the state", so "</description>"
- print chars after the "for " and before the ","
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to use cp find and grep together to copy a list of files using find with grep babhijit Linux - Newbie 10 07-03-2013 01:25 PM
How to grep sections of a line out and copy to new file Leisurely Gentlemen Linux - Newbie 4 05-08-2012 04:44 PM
grep a text in files and print the file name who don't contain such text whossa Linux - Newbie 5 04-13-2012 08:49 AM
using find or grep to find a group of text strings. tkmsr Linux - Newbie 3 03-04-2011 08:02 AM
Can I use GREP to find & replace text? jim.thornton Linux - Newbie 3 07-18-2008 07:36 PM


All times are GMT -5. The time now is 04:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration