-2

I need to find a particular pattern starting with 'abc.url.path=' and replace it with 'abc.url.path=https\://hostname.server.com.in\:8080/new'.

I tried the following:

sed -i "s/^abc.url.path=.*$/application.url.path=https/\://hostname/\:8080/new/" hello.txt

This gives me a sed character error. Please note I have tried using different delimeters. It does not solve the issue. I think I am missing the backslashes. I have also edited the details to highlight the exact issue.

Any help will be highly appreciated.

  • Try `sed -i 's,^abc\.url\.path=.*,application.url.path=https://hostname/:8080/new,' hello.txt` ([demo](https://ideone.com/ulO2Lc)). Or do you mean there must be literal ``\`` chars before `:`s? – Wiktor Stribiżew Jan 31 '20 at 13:06
  • Hi @tripleee I have tried to put different delimeters as well. It does not work. – Sourav Biswas Feb 03 '20 at 03:57
  • @WiktorStribiżew Yes, I actually need a few backslashes. I have provided a sample of the exact URL for replacing. – Sourav Biswas Feb 03 '20 at 04:03
  • But what wicked scenario could possibly require *colons* to be escaped? This "requirement" seems more likely to be a misunderstanding. – tripleee Feb 03 '20 at 04:45
  • No, it is not a misunderstanding or a mistake of update on my part. I am sorry to disappoint you. – Sourav Biswas Feb 03 '20 at 04:56
  • I got it to work. I tried the code mentioned by @WiktorStribiżew sed -i 's,^abc\.url\.path=.*,abc.url.path=https\://hostname.server.com.in\:8080/new,' Not sure why other delimeters like @ are not working. – Sourav Biswas Feb 03 '20 at 07:09

1 Answers1

0

For this test file

$ cat file
bc.url.path=
abc.url.path=https://hostname:8080
sdf.url.path=https://hostname

Usinng sed command

$ sed '/abc.url.path/cabc.url.path=https://hostname:8080/new' file
bc.url.path=
abc.url.path=https://hostname:8080/new
sdf.url.path=https://hostname

Sed explanation, command sed '/pattern/cnew_text' will change string with 'pattern' to 'new_text'

Ivan
  • 6,188
  • 1
  • 16
  • 23
  • I like this - using `/.../c...` instead of `s/.../.../` saves a lot of leaning-toothpick-syndrome. :) – Paul Hodges Jan 31 '20 at 18:03
  • Beware of the `.`s as they'll match any character and so your script will potentially change lines you didn't want changed. – Ed Morton Feb 01 '20 at 18:33
  • Also the `c` syntax is less portable; some `sed` dialects require whitespace after the command, etc. – tripleee Feb 04 '20 at 04:20