Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sed replace * in url with text in json file

I’m looking to change the value of a key in a json file

Example:

"House":"sample.house.city"

Need to change the word "sample" to something else.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I can do this with:

sed -i 's|sample.house.city|town-home.house.city|g' file.json

but there is no guarantee that "sample" is going to be the same every time.

I’ve tried:

sed -i 's|*.house.city|town-home.house.city|g' file.json

but it doesn’t change.

Also tried:

sed -i 's|"*.house.city"|"town-home.house.city"|g' file.json

but that ends up like:

"sample"town-home.house.city"

Any recommendations on how to do this correctly?

>Solution :

To match any word, you can use [[:alnum:]_]* (POSIX BRE) / [[:alnum:]_]+ (POSIX ERE):

sed -i 's|[[:alnum:]_]*\.house\.city|townhome.house.city|g' file.json

See the online demo:

#!/bin/bash
s='"House":"sample.house.city"'
sed 's|[[:alnum:]_]*\.house\.city|townhome.house.city|g' <<< "$s"

Output:

"House":"townhome.house.city"

Note you also need to escape dots that are used to match literal dots.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading