Re: Q: echo | sed -- Solved!
Re: Q: echo | sed -- Solved!
- Subject: Re: Q: echo | sed -- Solved!
- From: Gnarlodious <email@hidden>
- Date: Mon, 17 May 2004 23:28:00 -0600
It works!
Here it is as a one liner:
set someLines to do shell script "echo " & quoted form of someLines & " | tr
-s '\\r' '\\n' | sed '/2/,/5/d'"
In this script the quoted form is not necessary but in any real life script
characters will need escaping.
Apparently the unix line ending is necessary in this exact stream of
commands.
I don't know why the previous scripts worked for you but not for me or why
the sed substitution works from echo but not a span of lines. The
idiosyncracies of Applescript...
Thanks for the help, this will be a very useful trick for all kinds of jobs.
This script actually strips advertisements from Yahoo Group emails.
Thanks for the help!
-- Gnarlie
Entity Graff spoke thus:
>
The script I gave you works fine for me. I'm using Script Editor 2.0
>
(v43) with AppleScript 1.9.3.
>
>
The script that you have in your latest e-mail does not work, here's
>
what it looks like with the results of the "do shell script" commands:
>
----
>
set someLines to "1
>
2
>
3
>
4
>
5
>
6"
>
>
do shell script "echo " & someLines & " | tr -s '\\r' '\\n'" without
>
altering line endings
>
-->"1
>
-->"
>
>
do shell script "echo " & unixText & " | sed '/2/,/5/d'"
>
-->"sh: -c: line 2: syntax error near unexpected token `|'
>
-->sh: -c: line 2: ` | sed '/2/,/5/d''"
>
----
>
>
The line endings are still messing things up. You really need to use
>
the quoted form of a string when you pass it to a command like echo or
>
else the shell can misinterpret the string. That's why I added in the
>
"quoted form of" part to your script:
>
----
>
set someLines to "1
>
2
>
3
>
4
>
5
>
6"
>
>
set unixText to do shell script "echo " & quoted form of someLines & "
>
| tr -s '\\r' '\\n'" without altering line endings
>
unixText
>
-->"1
>
-->2
>
-->3
>
-->4
>
-->5
>
-->6
>
-->"
>
>
do shell script "echo " & quoted form of unixText & " | sed '/2/,/5/d'"
>
-->"1
>
-->6
>
-->"
>
----
>
>
As you can see adding "quoted form of" to convert the strings to quoted
>
strings allowed the script to work just fine.
>
>
- Ken
>
>
On May 17, 2004, at 10:00 PM, Gnarlodious wrote:
>
>
> That doesn't do a thing for me, does it work for you?
>
>
[...]
>
>
> This doesn't even work:
>
>
>
> set unixText to do shell script "echo " & someLines & " | tr -s '\\r'
>
> '\\n'
>
> " without altering line endings
>
>
>
> do shell script "echo " & unixText & " | sed '/2/,/5/d'"
>
>
>
>
>
> Maybe this functionality is broken in Script debugger?
>
>
>
> Entity Graff spoke thus:
>
>
>
>> You need to quote the string you are passing to the shell because of
>
>> the return characters. The return characters cause the command to be:
>
>> ----
>
>> echo 1<return>
>
>> 2<return>
>
>> 3<return>
>
>> 4<return>
>
>> 5<return>
>
>> 6 | sed 's/2/8/'
>
>> ----
>
>>
>
>> So the shell misinterprets your command. Here is a corrected version
>
>> of the script:
>
>> ----
>
>> set someLines to quoted form of "1
>
>> 2
>
>> 3
>
>> 4
>
>> 5
>
>> 6"
>
>>
>
>> do shell script "echo " & someLines & " | sed 's/2/8/'"
>
>> -->"1
>
>> -->8
>
>> -->3
>
>> -->4
>
>> -->5
>
>> -->6"
>
>>
>
>> do shell script "echo " & someLines & " | sed '/2/,/5/d'"
>
>> -->"1
>
>> -->6"
>
>> ----
>
>>
>
>> - Ken
>
>>
>
>> On May 17, 2004, at 6:38 PM, Gnarlodious wrote:
>
>>
>
>>> Why does the first command work but the 2nd doesn't?
>
>>>
>
>>> set someLines to "1
>
>>> 2
>
>>> 3
>
>>> 4
>
>>> 5
>
>>> 6"
>
>>>
>
>>> set replaceLine to do shell script "echo " & someLines & " | sed
>
>>> 's/2/8/'"
>
>>>
>
>>> set deleteRange to do shell script "echo " & someLines & " | sed
>
>>> '/2/,/5/d'"
>
>>>
>
>>>
>
>>> It operates on a text file just fine:
>
>>>
>
>>> do shell script "sed -e '/2/,/5/d' testFile"
>
>>>
>
>>> -- > 1
>
>>> -- > 6
>
>>>
>
>>>
>
>>> Am I doing something wrong?
>
_______________________________________________
>
applescript-users mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/applescript-users
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.