0

I have a string like
"solrHost" : "http://localhost:8983,http://localhost:8764", in a file called sampple.json. There could be space between "solrHost" and : or maybe not, and there could be space between : and "http://localhost:8983,http://localhost:8764"

I have a another variable newServerName="http://newserver,http://newserver2"

The value of solrHost could be anything between double quotes,

I want to replace the value of solrHost from old to newServerName using sed can anyone help me here ?

Inian
  • 80,270
  • 14
  • 142
  • 161

3 Answers3

2

You're much, much better off using a tool that understands JSON instead of trying to kludge together something with sed and regular expressions. jq is the go-to for command line manipulation of JSON:

$ cat foo.json
{
    "solrHost" : "http://localhost:8983,http://localhost:8764",
    "foo": 12
}
$ jq --arg url "http://newserver,http://newserver2" '.solrHost = $url' foo.json    
{
  "solrHost": "http://newserver,http://newserver2",
  "foo": 12
}
Shawn
  • 47,241
  • 3
  • 26
  • 60
  • @DavidC.Rankin I wonder if there's an answer as good as the classic ["Don't use regular expressions to parse XML"](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) one for JSON... – Shawn Dec 25 '19 at 09:01
  • I think that covers it. But you get some quirky questions that are not json, but sure look a lot like them. Either way with those quirky json-like formatted files, you are basically playing Russian-roulette without something like `jq` that is format aware and will validate the results. – David C. Rankin Dec 25 '19 at 09:04
1
sed 's%\("solrHost"[[:space:]]*:[[:space:]]*\)"[^"]*"%\1"'"$newServerName"'"%'
  • Use single quotes around most of the script.
  • Use % instead of / to mark the sections of the s/// (or s%%%) command.
  • Use [[:space:]]* to cover zero or more characters in the space class. Replace with just a blank-star if you don't care about the alternatives (tabs, etc), which is probably justifiable with well-formed JSON.
  • Capture the original "solrHost" part.
  • Be very careful with the quotes in the replacement.
    • "'"$newServerName"'"
    • The first double quote will appear in the replacement text.
    • The first single quote terminates the current single-quoted string.
    • The second double quote starts a new double-quoted string.
    • The replacement variable is next.
    • The third double quote ends the double-quoted string.
    • The second single quote starts a new (and rather short) single-quoted string.
    • The fourth double quote will appear in the replacement text.
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • That depends more on the layout of the fields. The chances are it would be OK, but if the JSON is on a single line, the simpler code could replace the wrong info whereas the admittedly more complex code in the answer would not make that mistake. – Jonathan Leffler Dec 25 '19 at 09:01
  • You are correct and you would definitely need the alternative substitute delimiters, e.g. `sed '/^"solrHost"/s@"http.*$@'"$newServerName"'@'` – David C. Rankin Dec 25 '19 at 09:02
-1

Use below sed command to replace the text .

%s/solrHost(old name)/newServerName/g
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93