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: Gnarlodious <email@hidden>
- Date: Mon, 24 Jan 2005 18:10:38 -0700
Entity Graham Anderson spoke thus:
> :)
> this worked.....
> seemed that setting theScript string before 'do shell script' made it
> work ....
Shouldn't be necessary.
> set mypath to (path to preferences) & "Fonovisa.xml" as string
> set thepath to POSIX path of mypath
> try
> set thestring to ""
> set theScript to "cat " & thepath & "| tr '\\r' '\\n' | sed -n
> '/<ram>/{s/.*<ram>\\(.*\\)<\\/ram>.*/\\1/p;}'"
I only included "| tr '\\r' '\\n'" so the example would work in Script
Editor. If your XML file is a UNIX file (and it should be) you can disregard
that snippet.
Also, you don't need to "cat" the whole file into sed because sed operates
linewise on the file. That means you can drop the pipe and just put your
filepath after the sed command which is a lot easier to read and runs
faster.
Piping "cat" into a shell script can be useful when you need to manipulate
an entire file but for reading one string it's unnecessary.
> set thestring to do shell script theScript
> return thestring
> on error err
> log ("Cat text file error: " & err)
> end try
> I really need to pick up a Unix book......
It looks like the previous section could be optimized. Exactly what are you
trying to do?
For example, if the file contains no such string you could say:
set someString to do shell script "sed -n
'/<ram>/{s/.*<ram>\\(.*\\)<\\/ram>.*/\\1/p;}'
~/Library/Preferences/Fonovisa.xml""
if someString = "" then display dialog "No such XML item"
someString
-- Gnarlie
> On Jan 24, 2005, at 4:16 PM, Roger Howard wrote:
>>
>> On Jan 24, 2005, at 4:01 PM, Graham Anderson wrote:
>>
>>> this does not quite work...but is it a bit closer ?
>>> many thanks
>>>
>>>
>>>
>>> set my_path to (path to preferences) & "Fonovisa.xml" as string
>>> set thepath to POSIX path of my_path
>>>
>>> loadTextFile(thepath)
>>>
>>> on loadTextFile(thepath)
>>> try
>>> set thestring to ""
>>> set theScript to ("cat " & "'" & thepath & "'")
>>> set thestring to do shell script theScript
>>> --return thestring
>>> set gimme to do shell script "echo " & thestring & " | tr '\\r'
>>> '\\n' | sed -n '/<ram>/{s/.*<ram>\\(.*\\)<\\/ram>.*/\\1/p;}' "
>>> return gimme
>>> on error err
>>> log ("Cat text file error: " & err)
>>> end try
>>> end loadTextFile
>>
>> Grep... no fun to me :)
>>
>> Why not use tools that understand XML if you're trying to parse XML?
>> Admittedly you didn't post an example of the whole XML doc... but
>> here's a simple example using LateNightSW's XML Tools for parsing a
>> VERY simple XML doc.
>>
>> Put the following into a file and save it:
>>
>> <ram>512</ram>
>>
>> Now install XMLTools and run this from your script editor:
>>
>> item 1 of XML contents of (parse XML (choose file))
>>
> _______________________________________________
_______________________________________________
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