Discussion:
sed ssed bug with the windows binaries
Guillaume Frambourg
2009-11-13 09:46:22 UTC
Permalink
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
2009-11-13 10:01:42 UTC
Permalink
Hi again

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

Works as intended.

Sry for disturbing.
Post by Guillaume Frambourg
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.
test.txt contains
"hello"
--------------------------------------------------------
sed -e "s/\"//g" test.txt
--------------------------------------------------------
--------------------------------------------------------
hello
--------------------------------------------------------
--------------------------------------------------------
sed -e "s/\"//g" test.txt > test2.txt
--------------------------------------------------------
--------------------------------------------------------
hello
sed: can't read >: Invalid argument
--------------------------------------------------------
--------------------------------------------------------
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
Eli Zaretskii
2009-11-14 09:20:15 UTC
Permalink
Date: Fri, 13 Nov 2009 10:46:22 +0100
--------------------------------------------------------
sed -e "s/\"//g" test.txt
--------------------------------------------------------
--------------------------------------------------------
hello
--------------------------------------------------------
--------------------------------------------------------
sed -e "s/\"//g" test.txt > test2.txt
--------------------------------------------------------
--------------------------------------------------------
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
2009-11-14 11:43:28 UTC
Permalink
Post by Eli Zaretskii
Date: Fri, 13 Nov 2009 10:46:22 +0100
--------------------------------------------------------
sed -e "s/\"//g" test.txt
--------------------------------------------------------
--------------------------------------------------------
hello
--------------------------------------------------------
--------------------------------------------------------
sed -e "s/\"//g" test.txt > test2.txt
--------------------------------------------------------
--------------------------------------------------------
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.
Post by Eli Zaretskii
The escape character of the Windows shell is `^'. However, it doesn't
escape inside quoted strings :-(. So you need to get creative. For
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
Loading...