0

I've been tasked with migrating a group of batch scripts to Windows 7 (from XP) and have had a few problems using sed for substitution. What i need the line to do is find LogPath and anything inside the double quotes should be replaced with ABC (just for testing - will actually be a UNC path).

However instead I'm getting two strange problems:

  • it's deleting the first double quote
  • more importantly it isn't actually replacing anything inside the quotes, but instead is just appending to this string

Here is the relevant line of the script:

sed \\fs-bri-01\9732\9732.hfls -i -e s,LogPath="*.",LogPath="ABC",g

This script works on Windows XP but not Windows 7.

Vicky
  • 12,934
  • 4
  • 46
  • 54
ION
  • 177
  • 1
  • 3
  • 16

2 Answers2

0

Maybe the problem comes from the UNC path:

pushd \\fs-bri-01\9732
sed 9732.fls -i -e s,LogPath="*.",LogPath="ABC",g
popd

But maybe the problem comes from the quote characters and the way sed gets its argv array. Then you can try:

sed -i -e "s/LogPath=\".*\"/LogPath=\"ABC\"/g" \\fs-bri-01\9732\9732.hfls
Benoit
  • 76,634
  • 23
  • 210
  • 236
  • I wouldn't expect it to be the UNC path due to the script actually editing the file just not how i want it. The quotes are a standard part of regex which are used to find anything inside the quote marks so wouldn't think it would be that but can't be sure. Well i would try to use a sed script file if i knew what extension that is :D. But really trying to avoid having this in a seperate file as this script already has 5/6 script files related to it. – ION Sep 29 '11 at 13:10
  • Ahhh escape characters that's what i was missing :P. Lol mind u doesn't exactly work instead it does replace what's inside the double quotes but also removes everything else off the line :P. Is it me or is it acting a little loopy ? – ION Sep 29 '11 at 13:19
  • @user914244: sed is greedy. So `.*"` will match everything until the LAST double quote. If you want non-greedy matching then use Perl for example: `perl -pe "s/LogPath=\".*?\"/LogPath=\"ABC\"/g;"` – Benoit Sep 29 '11 at 13:23
  • No way i can do non-greedy in a batch script ? – ION Sep 29 '11 at 13:32
  • Well figured a work around posting it below for anyone stuck in the same situation. sed \\fs-bri-01\9732\9732.hfls -i -e "s/LogPath=\"[\:A-Z0-9a-z\_\\\/\.\ ]*\"/LogPath=\"ABC\"/g" – ION Sep 29 '11 at 13:37
  • @user914244 : you can post your answer (nicely formatted please ;-) and accept your answer. Then you can up-vote your answer. Both of these actions increase your reputation points here on S.O. Good luck. – shellter Sep 30 '11 at 01:31
0

Well figured a work around posting it below for anyone stuck in the same situation.

sed \\fs-bri-01\9732\9732.hfls -i -e "s/LogPath=\"[\:A-Z0-9a-z_\\\/\.\ ]*\"/LogPath=\"ABC\"/g"
ION
  • 177
  • 1
  • 3
  • 16