2

My environment:

I am working with the command interpreter on Windows.

I use an sh.exe that came with some proprietary tools I have installed. I do not know the details of that sh.exe but here is what I get when querying its version:

>sh.exe --version
GNU bash, version 3.1.23(1)-release (i686-pc-msys)
Copyright (C) 2005 Free Software Foundation, Inc.

Similarly, here is what I have for sed:

>sed --version
GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.

GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <[email protected]>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.

What I am after:

I would like to be able to use a sed command that matches for the equal character and do so from a shell script.

This works as expected:

>echo "a=b" | sed "s/a=//"
"b"

If parse_trace.sh contains

#!/bin/sh

sed "s/a//"

I also get what I expect:

>echo "a=b" | sh parse_trace.sh
"=b"

However, replacing the last line of my script with:

sed "s/a=//"

leads to

>echo "a=b" | sh parse_trace.sh
sed.exe: -e expression #1, char 5: unterminated `s' command

And if I use instead

sed "s/a\=//"

I get

>echo "a=b" | sh parse_trace.sh
"==b"

What am I missing?

Update

The = sign is correctly interpreted within my shell script if it isn't the last character in the pattern. I.e. if my last line in the script is now:

sed "s/a=b//"

Then I get:

echo "a=b" | sh parse_trace.sh
""

echo "a=bc" | sh parse_trace.sh
"c"

So there is something about the =/ that makes the DOS-SHELL combo fail somehow.

Update 2

>od -Ax -tx1z parse_trace.sh
000000 23 21 2f 62 69 6e 2f 73 68 0a 0a 73 65 64 20 22  >#!/bin/sh..sed "<
000010 73 2f 61 5c 3d 2f 2f 22 0a 0a                    >s/a\=//"..<
00001a

>echo "a=b" | sh -x parse_trace.sh
+ sed 's/a\=//'
"==b"

>od -Ax -tx1z parse_trace.sh
000000 23 21 2f 62 69 6e 2f 73 68 0a 0a 73 65 64 20 22  >#!/bin/sh..sed "<
000010 73 2f 61 3d 2f 2f 22 0a 0a                       >s/a=//"..<
000019

>echo "a=b" | sh -x parse_trace.sh
+ sed s/a=//
sed.exe: -e expression #1, char 5: unterminated `s' command
8
  • Are you missing = from your last couple of seds? Nov 23, 2015 at 15:03
  • Indeed: the = disappeared when I composed my question (which is ironic given what I am trying to do!). I modified my post. And yes, I am working on DOS as stated in the post.
    – Lolo
    Nov 23, 2015 at 15:06
  • 1
    I suspect this has something to do with the DOS shell parsing /option=value in command arguments. Maybe if you posted in a more appropriate site, someone would really know what's going on.
    – Barmar
    Nov 23, 2015 at 15:56
  • Weird. While I understand Barmar's hunch, I also think DOS' command line parsing should not be involved, as the sh interpreter is used to interpret the script. Perhaps the OP could try running their entire command within sh and see if anything changes?
    – dhag
    Nov 23, 2015 at 16:13
  • sed and sh aren't, AFAIK, normally included with DOS (or Windows). Where did you get those binaries? (Seems like you might just be running into a bug in one of them.)
    – derobert
    Nov 23, 2015 at 16:13

1 Answer 1

0

I'm not sure it will works, but I think it's a good shot:

sed 's,a\x3d,,'

I tried it in my environment, it works. There are two things might be the reason you got that situation:
a. The sh you are using and the cmd together treat '/' and '\' both delimiter of sed.
b. You are using double quotes, which bash will try to find equation and vars inside it.

I myself use cmd combined with shell commands a lot, I would recommend you try cygwin. Add cygin's bin path to PATH of windows, will works like a charm. But you need be careful about ESCAPE.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .