I have a java properties file that looks like the following:
sed -i 's:^[ \t]*siteUrlEndpoint[ \t]*=\([ \t]*.*\)[/]*$:siteUrlEndpoint = 'amazon.com':' file
You can do it with two backreferences, e.g.
sed -i.bak 's|^\(SiteUrlEndpoint=\).*/\(.*\)|\1amazon.com/\2|' file
note: the match of text up to
/ is greedy. If you have multiple parts of the path following the domain, you probably want to preserve all path components. To make it non-greedy, you could use the following instead
sed -i.bak 's|^\(SiteUrlEndpoint=\)[^/]*/\(.*\)|\1amazon.com/\2|' file
(you can add
i.bak to create a backup of the original in
To accomplish the same thing, you can match
SiteUrlEndpoint= at the beginning of the line first, and then use a single backreference for the change, e.g.
sed -i.bak '/^SiteUrlEndpoint=/s|=[^/]*\(/.*\)|=amazon.com\1|' file
For example, given a file
$ cat sites SiteUrlEndpoint=google.com/path/to/mySite SiteUrlSomeOther=google.com/mySite
You can change
amazon.com with (using non-greedy form of first example):
$ sed -i 's|^\(SiteUrlEndpoint=\)[^/]*/\(.*\)|\1amazon.com/\2|' sites
$ cat sites SiteUrlEndpoint=amazon.com/path/to/mySite SiteUrlSomeOther=google.com/mySite
$ cat sites.bak SiteUrlEndpoint=google.com/path/to/mySite SiteUrlSomeOther=google.com/mySite
Explanation (first form)
sed -i.bak 's|^\(SiteUrlEndpoint=\)- locate & save
[^/]*/- match any folowing characters up to first
/(non-greedy - adjust as needed)
\(.*\)- match and save anything following
|\1amazon.com/\2|'- full replacement (explanation below)
\1- first back-reference containing
'/'second back-reference of everything that followed.
Look over all the solutions and let me know if you have questions.