Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: sed ssed bug with the windows binaries

1 view
Skip to first unread message

Guillaume Frambourg

unread,
Nov 13, 2009, 5:01:42 AM11/13/09
to bug-gn...@gnu.org
Hi again

Found a workarround :
command :
------------------------------
sed -e "s/\"//g" "test.txt>test2.txt"
------------------------------

Works as intended.

Sry for disturbing.

2009/11/13 Guillaume Frambourg <frambourg...@gmail.com>

> Hi,
>
> When you try to replace " with some character that is not escaped, it
> works. But it won't accept to save the output in another file.
>
> ie :
>
> test.txt contains
>
> "hello"
>
> Working sed is :
> --------------------------------------------------------
> sed -e "s/\"//g" test.txt
> --------------------------------------------------------
>
> it outputs :
> --------------------------------------------------------
> hello
> --------------------------------------------------------
>
> But if you try :
> --------------------------------------------------------
> sed -e "s/\"//g" test.txt > test2.txt
> --------------------------------------------------------
>
> it outputs :
> --------------------------------------------------------
> hello
> sed: can't read >: Invalid argument
> --------------------------------------------------------
>
> Also trying :
> --------------------------------------------------------
> sed -e "s/\"/\"test/g" test.txt > test2.txt
> --------------------------------------------------------
>
> it outputs "testhello"test into test2.txt
>
>
> So using an escaped character in search string but not in replacestring
> breaks the command line interpretation.
>
> Thanks
>

Guillaume Frambourg

unread,
Nov 13, 2009, 4:46:22 AM11/13/09
to bug-gn...@gnu.org

Eli Zaretskii

unread,
Nov 14, 2009, 4:20:15 AM11/14/09
to Guillaume Frambourg, bug-gn...@gnu.org
> Date: Fri, 13 Nov 2009 10:46:22 +0100
> From: Guillaume Frambourg <frambourg...@gmail.com>

>
> Working sed is :
> --------------------------------------------------------
> sed -e "s/\"//g" test.txt
> --------------------------------------------------------
>
> it outputs :
> --------------------------------------------------------
> hello
> --------------------------------------------------------
>
> But if you try :
> --------------------------------------------------------
> sed -e "s/\"//g" test.txt > test2.txt
> --------------------------------------------------------
>
> it outputs :
> --------------------------------------------------------
> hello
> sed: can't read >: Invalid argument

That's not a problem with Sed, this is a problem with the Windows
shell's too naive handling of quotes. The Windows shell doesn't
understand the backslash escape, so it thinks the second quote closes
the first one, and the 3rd one quotes everything up to the end of the
command.

The escape character of the Windows shell is `^'. However, it doesn't
escape inside quoted strings :-(. So you need to get creative. For
example:

sed -e "s/"^""//g" test.txt > test2.txt

This quotes parts of the Sed expression separately, with the literal
quote escaped by `^'.


Manuel Collado

unread,
Nov 14, 2009, 6:43:28 AM11/14/09
to
Eli Zaretskii escribi�:

>> Date: Fri, 13 Nov 2009 10:46:22 +0100
>> From: Guillaume Frambourg <frambourg...@gmail.com>
>>
>> Working sed is :
>> --------------------------------------------------------
>> sed -e "s/\"//g" test.txt
>> --------------------------------------------------------
>>
>> it outputs :
>> --------------------------------------------------------
>> hello
>> --------------------------------------------------------
>>
>> But if you try :
>> --------------------------------------------------------
>> sed -e "s/\"//g" test.txt > test2.txt
>> --------------------------------------------------------
>>
>> it outputs :
>> --------------------------------------------------------
>> hello
>> sed: can't read >: Invalid argument
>
> That's not a problem with Sed, this is a problem with the Windows
> shell's too naive handling of quotes. The Windows shell doesn't
> understand the backslash escape, so it thinks the second quote closes
> the first one, and the 3rd one quotes everything up to the end of the
> command.

The DJGPP port of set works OK, because it parse the command line
itself. But both the Cygwin and the MinGW ports emit the given error
message.

>
> The escape character of the Windows shell is `^'. However, it doesn't
> escape inside quoted strings :-(. So you need to get creative. For
> example:
>
> sed -e "s/"^""//g" test.txt > test2.txt
>
> This quotes parts of the Sed expression separately, with the literal
> quote escaped by `^'.

This does not work for me. It seems that quotes in the command line must
always be in pairs. I.e. there must be an even number of quotes in each
argument. The workaround is to use the octal escape code for the quote
character:

sed -e "s/\042//g" test.txt > test2.txt


Hope this helps.
--
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado

0 new messages