0

So I have the following in a file:

/icon.png
/my/path/tester/icon.png
/logo.png
/my/path/tester/logo.png

I want to replace all lines that do NOT contain 'tester' and add '/my/path/tester/' to the beginning... leaving:

/my/path/tester/icon.png
/my/path/tester/icon.png
/my/path/tester/logo.png
/my/path/tester/logo.png

I would also prefer to edit the file in place using sed.

Thanks in advance!

user2274114
  • 27
  • 1
  • 5

1 Answers1

2

If you have a sed that supports the -i option (eg, gnu-sed):

sed -i '/tester/!s@^@/my/path/tester@' file

Note that this does what you ask for, but is not robust. You probably want to limit the replacement to lines that do not match '/tester/' rather than 'tester', and there really is no point to the -i option (see sed edit file in place). Using -i obfuscates the temporary file, which for some reason people often do not want and think they avoid by using -i.

Community
  • 1
  • 1
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • Thanks! Works perfect from the cmd line but within a script it throws: unterminated `s' command. Any thoughts? – user2274114 Aug 13 '13 at 02:15
  • 1
    Invalid quoting inside the script, or perhaps the script is used to generate another and '@@' has special meaning (eg, @...@ indicates a text replacement). Otherwise, need more details on the script. Try a different delimter. (eg, replace @ with ! or something else) – William Pursell Aug 13 '13 at 03:26