12

I'm new to shell scripting but getting this error and cant figure out what up.

sed: 1: "/Users/local/Do ...": extra characters at the end of d command
sed: 1: "/Users/local/Do ...": extra characters at the end of d command
sed: 1: "/Users/local/Do ...": extra characters at the end of d command
sed: 1: "/Users/local/Do ...": extra characters at the end of d command

Here is the script I'm running

for fl in $(S_convertPath ${RESOURCE_DIR}/db)/db.changelog-*xml; do
    sed -i "s/HOSTNAME/${IP_ADDRESS}/g" $fl
done

Thanks

daverocks
  • 2,293
  • 5
  • 19
  • 23

1 Answers1

46

Given that sed seems to think you're running a delete line command (d), you may want to output the command to see what's actually in that environment variable of yours:

for fl in $(S_convertPath ${RESOURCE_DIR}/db)/db.changelog-*xml; do
    echo sed -i "s/HOSTNAME/${PSM_SERVER_ADDRESS}/g" $fl
done

There's a good chance the PSM_SERVER_ADDRESS is corrupting your sed command (and may need to be processed to make it clean). One way to do this (provided you have a recent enough sed) would be to use delimiters that do not appear in the environment variable, for example:

sed -i "s?HOSTNAME?${PSM_SERVER_ADDRESS}?g" $fl

Since you've accepted this answer, I may as well add the resolution for the additional problem you found. It appears that BSD sed, unlike Linux, has a requirement that you provide an extension to the -i option. So, while Linux allows:

sed -i "sed-command"

to edit in-place, the BSD variant needs to have:

sed -i "" "sed-command"

with an empty backup suffix.

Without that, sed may use your command as the extension and your first file name as the command.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 2
    It is. The value `PSM_SERVER_ADDRESS` is a path (which contains `/` characters) so the s command interprets those `/` characters as its delimiters (e.g., `s/HOSTNAME//Users/davidkavanagh/path/g`. Looks like alternate delimiters should be used. Perhaps `s@HOSTNAME@${PSM_SERVER_ADDRESS}/g`. – jamessan Oct 04 '11 at 13:14
  • IP_ADDRESS is equal to a function that gets ip using hostname -i for use across all platforms – daverocks Oct 04 '11 at 15:34
  • @daverocks, you _still_ need to run the modified script I provided to get the line echoed, even with the changes you have made. It's the only way to know what the commands being given to `sed` are. – paxdiablo Oct 04 '11 at 23:46
  • @paxdiablo Yep, I had recently encountered such issue in one of my sed script. Changing the delimiter works. Sed gets confused when it passes the variable value to the substitution field that happens to look like '/'. My preference is an underscore '_' but you can use '?' too. – jaypal singh Oct 05 '11 at 00:45
  • Sorry, ran debugging with echo and it printed what was expecting it got IP address and listed the 4 files paths to change. 'sed -i s/HOSTNAME/12.27.2.12 /g /Users/davidkava/Docu.....og-1.0.xml' I tried delimiter ? and @ with no joy so going to research more thanks for help – daverocks Oct 05 '11 at 08:24
  • 3
    found solution here http://stackoverflow.com/questions/4247068/sed-command-failing-on-mac-but-works-on-linux had to use single quotes after -i, thanks for help – daverocks Oct 05 '11 at 09:37