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 ?

1
  • 1
    Rule 1 of working with JSON: Use a tool that understands the format, like jq. Commented Dec 25, 2019 at 8:39

3 Answers 3

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

@DavidC.Rankin I wonder if there's an answer as good as the classic "Don't use regular expressions to parse XML" one for JSON...
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.
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.

2 Comments

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.
You are correct and you would definitely need the alternative substitute delimiters, e.g. sed '/^"solrHost"/s@"http.*$@'"$newServerName"'@'
-1

Use below sed command to replace the text .

%s/solrHost(old name)/newServerName/g

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.