0

I am trying to search for a pattern using SED and trying to replace some text (IP Address) after = which is in between of the line. But it is replacing till the end of the line where I need to ignore the last character in the line which is ".

This is my file where I need the update to be done on.

$ cat appsettings.json
......
..........
"ConnectionStrings": {
    "PMIEnterpriseDbLocal":"Data Source=ec2-123-000-111-123.us-east-1.compute.amazonaws.com;Initial Catalog=FunnyTestDb;Persist Security Info=True;User ID=sa;Password=W@rld111"
},
......
.......
"Profile": "testing",
..............
.....
END OF FILE

I need 3 values to be replaced in the above file using SED.

  1. Source = <someIP> (should replace the IP till the ;)
  2. Password = <someText> (should ignore the last character ")
  3. Profile = <someValue> (should replace the value in the "")

I tried with the below commands to search for the 3 patterns:

sed -i -r 's#Source=[^ ;]*#Source=10.10.10.10#' appsettings.json"

SED used for Source pattern is working fine as expected (which mean I am able to replace the value with the provided IP address)

sed -i -r 's#Password=[^ ]*#Password=abc@123#' appsettings.json"

SED used for Password is working but the last character (which is ") in that line is also getting replaced, I only need the password to be replaced and last character (which is ") should be ignored.

sed -i -r 's#Profile:["^ "]*#Profile:"sample"#' appsettings.json"

SED should search for the pattern Profile in the file and replace the text which I pass

Expected executing the SED command, I should see the the updated appsetting file as below:

$ cat appsettings.json
......
..........
"ConnectionStrings": {
    "PMIEnterpriseDbLocal":"Data Source=10.10.10.10;Initial Catalog=FunnyTestDb;Persist Security Info=True;User ID=sa;Password=abc@123"
},
......
.......
"Profile": "sample",
..............
.....
END OF FILE
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
phani
  • 65
  • 1
  • 6
  • Use the proper tool for the job. Possible duplicate of [Parsing JSON with Unix tools](https://stackoverflow.com/q/1955505/608639), [How to parse JSON with shell scripting in Linux?](https://stackoverflow.com/q/1955505/608639), [Read JSON data in a shell script](https://stackoverflow.com/q/20488315/608639), etc. – jww Jul 08 '19 at 20:10
  • `ec2-123-000-111-123.us-east-1.compute.amazonaws.com` is not an IP address. It looks like a hostname. IP addresses look like n.n.n.n (IPv4) or xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx (IPv6) – William Pursell Jul 08 '19 at 21:49

1 Answers1

1

For this particular snippet the following works:

sed  -i -r -e 's#Source=[^ ;]*#Source=10.10.10.10#' -e 's#Password=[^ "]*#Password=abc@123#' -e 's#"Profile": "[^ "]*"#"Profile": "sample"#' appsettings.json
.....
.........
"ConnectionStrings": {
    "PMIEnterpriseDbLocal":"Data Source=10.10.10.10;Initial Catalog=FunnyTestDb;Persist Security Info=True;User ID=sa;Password=abc@123"
},
......
.......
"Profile": "sample",
............
......
END OF FILE
tink
  • 14,342
  • 4
  • 46
  • 50