user2274114
user2274114

Reputation: 27

Sed to replace first forward slash in line after match with a string

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!

Upvotes: 0

Views: 510

Answers (1)

William Pursell
William Pursell

Reputation: 212208

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.

Upvotes: 2

Related Questions