tags:

views:

1822

answers:

5

Edit: I'm using CYGWIN/GNU sed version 4.1.5 on windows Vista and I want a case insensitive search

I want to use sed to replace inline, the following:

c:\DEV\Suite\anything here --- blah 12 334 xxx zzzzz etc\Modules etc

Edit: anything here --- blah 12 334 xxx zzzzz etc means anything could appear here. Sorry for omitting that.

In a file with lines like

FileName="c:\DEV\Suite\anything here --- blah 12 334 xxx zzzzz etc\Modules\.... snipped ...."

with a value I supply, say :

Project X - Version 99.98

So the file ends up with:

FileName="c:\DEV\Suite\Project X - Version 99.98\Modules\.... snipped ...."

My attempt:

c:\temp>sed -r -b s/Dev\\Suite\\.*\\Modules/dev\\suite\\simple\\/g test.txt

However I get the following error:

sed: -e expression #1, char 42: unterminated `s' command

Thanks. Edit: I've already tried added quotes.

+2  A: 

It's the '\\' before the '/'. Apparently you need 4 backslashes.

sed -r -b "s/Dev\\\\Suite\\\\.*\\\\Modules/dev\\\\suite\\\\simple\\\\/g" test.txt

I think the shell is interpreting the '\\' into a '\' before passing it to sed, and then sed is doing the same thing on what it gets.

Single quotes would work, so:

sed -r -b 's/Dev\\Suite\\.*\\Modules/dev\\suite\\simple\\/g' test.txt
retracile
Nope same result.
Preet Sangha
Ah, on second look, I see the problem. Answer updated.
retracile
Sorry same problem
Preet Sangha
Actually sorry: The double quotes does work but the single one doesn't.
Preet Sangha
I tested quotes and backslashes on a WinXP cygwin bash shell. What shell are you using?
retracile
I'm using cmd.exe
Preet Sangha
A: 

Got it: Replace the .* with .+

sed -r -b s/Dev\\Suite\\.+\\Modules/dev\\suite\\simple\\/g test.txt
Preet Sangha
If that works, then the likely problem was your shell expanding .* . The quotes should have prevented the expansion.
brianegge
A: 

I don't know what version of sed your using. I'm not familiar with the -b option.

First, I'd suggest using the i regex flag, to make it case insensitive. Your example of DEV won't match your regex of Dev.

I suspect the problem your running into is how your version of sed interprets backslash characters.

I'd suggest using the sed bundled with Cygwin. With single quotes, it seems to work for me.

echo 'c:\DEV\Suite\anything here --- blah 12 334 xxx zzzzz etc\Modules\' | sed -r 's/Dev\\Suite\\.*\\Modules/dev\\suite\\simple\\/gi'
c:\dev\suite\simple\\
brianegge
I wasn't familiar with -b either. Appears to be a cygwin thing to prevent conversion of lineendings.
retracile
A: 

If I use "\\\" where you have "\\", it works for me. With the double backslashes, the way it gets parsed evidently has a backslash escaping the terminating "/" of the substitution expression. (I still get the error if I replace ".*" with ".+".)

(Amusingly, I had to add more backslashes to get this to post properly -- SO ate a few of them!)

Jim Lewis
just to be clear:I change all the double back slashes to triples?
Preet Sangha
Yes, that's what worked for me. I forgot to mention that I also used double-quotes around the expression.
Jim Lewis
A: 

well...

sed -e s/"anything here --- blah 12 334 xxx zzzzz etc"/"Project X - Version 99.98"/g test.txt

worked fine

(The compliant about the unterminated 's' was because of the unescaped '/')

Ryan Fernandes