LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 03: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, 03:50 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Should be simple. Can you provide a bit more of the XML that is related to what you requested?
 
Old 07-16-2014, 05:02 PM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,147

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
Perl would be better:

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

Rep: Reputation: 275Reputation: 275Reputation: 275
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, 06: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 06:50 AM.
 
Old 07-17-2014, 07:17 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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



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 12:25 PM
How to grep sections of a line out and copy to new file Leisurely Gentlemen Linux - Newbie 4 05-08-2012 03: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 07:49 AM
using find or grep to find a group of text strings. tkmsr Linux - Newbie 3 03-04-2011 07:02 AM
Can I use GREP to find & replace text? jim.thornton Linux - Newbie 3 07-18-2008 06:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:58 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
Open Source Consulting | Domain Registration