john john - 1 month ago 5x
Bash Question

Using sed to replace text within a java properties file

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

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 then the result would look like:

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 = '':' file


You can do it with two backreferences, e.g.

sed -i.bak 's|^\(SiteUrlEndpoint=\).*/\(.*\)|\\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=\)[^/]*/\(.*\)|\\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|=[^/]*\(/.*\)|\1|' file

For example, given a file sites containing:

$ cat sites

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

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


$ cat sites


$ cat sites.bak

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. |\\2|' - full replacement (explanation below)
  5. \1 - first back-reference containing SiteUrlEndpoint=
  6. - 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.