Re: using grep in a do shell script
Re: using grep in a do shell script
- Subject: Re: using grep in a do shell script
- From: "Adam K. Wuellner" <email@hidden>
- Date: Mon, 24 Jan 2005 18:07:16 -0600
On Jan 24, 2005, at 5:07 PM, Graham Anderson wrote:
if I have an xml tag
<ram>512</ram> in an xml file...
how can I get the '512' out of it
this is what I have so far...currently it returns the whole xml file :(
set my_path to (path to preferences) & "Fonovisa.xml" as string
set myfile to POSIX path of my_path
set theScript to "grep ram " & myfile
--return theScript
display dialog (do shell script theScript)
Option 1:
You have an XML file. Use an XML parser.
XMLLib osax 2.0, from Emmanuel's people:
<http://www.satimage.fr/software/en/downloads_osaxen.html>
XML Tools 2.7, from Late Night Software
<http://latenightsw.com/freeware/XMLTools2/index.html>
example: <http://latenightsw.com/freeware/XMLTools2/asUtilities.html>
Option 2:
Fix your grep. Sounds like the XML file does not have line-endings that
grep recognizes. It sees the whole file as one line, therefor, and
finds the match and returns the line that contains it (the whole file).
Try replacing the carriage returns (\r) with line-feeds (\n).
do shell script "cat Fonovisa.xml | tr '\\r' '\\n' | grep ram"
-- should return the entire line containing "<ram>512</ram>"
You still need to parse the returned text to get rid of the enclosing
tags.
Option 3:
Use a different CL utility, one that will match the element, and return
the value only.
Example, using Perl (sed or awk would work as well):
do shell script "cat Fonovisa.xml | tr '\\r' '\\n' | perl -ne 'print
\"$1\" if m|<ram>(.*?)</ram>|'"
-- should return '512'
HTH,
Adam
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden