Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use sed to replace values in json file variable value

Tags:

bash

sed

I need to use sed to modify the 3rd line in my.json file as line shown below.

How do I replace a changing key value( this example shows "group-1") with the variable value that is assigned to $username?

    "name": "group-1",
like image 526
ikask Avatar asked Aug 31 '25 02:08

ikask


1 Answers

You can replace like below

Linux

 sed -i 's/\"name\":.*/\"name\": '${username}'/g' "/path/to/my.json"

MacOS

   sed -i "" 's/\"name\":.*/\"name\": '${username}'/g' "/path/to/my.json"
like image 145
ntshetty Avatar answered Sep 03 '25 00:09

ntshetty