john john - 3 months ago 11
Bash Question

Using sed to replace text within a java properties file

I have a java properties file that looks like the following:

SiteUrlEndpoint=google.com/mySite


I want to use sed -i to inline replace the url but keep the context path that comes out of it. So for example if I wanted to change the properties file above to use amazon.com then the result would look like:

SiteUrlEndpoint=amazon.com/mySite


I am having trouble with sed to only replace the url and keeping the context path when replacing it inline.

My attempt:

sed -i 's:^[ \t]*siteUrlEndpoint[ \t]*=\([ \t]*.*\)[/]*$:siteUrlEndpoint = 'amazon.com':' file

Answer

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 file.bak)

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 sites containing:

$ cat sites
SiteUrlEndpoint=google.com/path/to/mySite
SiteUrlSomeOther=google.com/mySite

You can change google.com to amazon.com with (using non-greedy form of first example):

$ sed -i 's|^\(SiteUrlEndpoint=\)[^/]*/\(.*\)|\1amazon.com/\2|' sites

Confirming:

$ cat sites
SiteUrlEndpoint=amazon.com/path/to/mySite
SiteUrlSomeOther=google.com/mySite

and

$ cat sites.bak
SiteUrlEndpoint=google.com/path/to/mySite
SiteUrlSomeOther=google.com/mySite

Explanation (first form)

  1. sed -i.bak 's|^\(SiteUrlEndpoint=\) - locate & save SiteUrlEndpoint=
  2. [^/]*/ - match any folowing characters up to first / (non-greedy - adjust as needed)
  3. \(.*\) - match and save anything following /
  4. |\1amazon.com/\2|' - full replacement (explanation below)
  5. \1 - first back-reference containing SiteUrlEndpoint=
  6. amazon.com - self-explanatory
  7. /\2 - the '/' second back-reference of everything that followed.

Look over all the solutions and let me know if you have questions.

Comments