Issue
we try to replace the content of the third field that comes after group word ,
the following line in file is always with the same syntax except third field that change
"groups": [{"group": "string”}],
The third field contains any characters of combination of A-Z
, a-z
, numbers , ( _ - .
)
The third field is always in double quotes
Example
"groups": [{"group": "MOVIES.ARE.GOOD”}],
"groups": [{"group": "no_one.ok”}],
"groups": [{"group": "evry_one.lol”}],
"groups": [{"group": "NO_VALUES_ARE-GOOD”}],
"groups": [{"group": "___scripts”}],
etc
I try with the following sed , but without success
var=my.word__that.I.need.to.replace
sed -i "s/\(group\": \"\)[*]*/\1$var/" file
Solution
Assuming:
- all quotes are
"
. See the comment from @Gordon above. - you want to replace the 3rd field in each line.
- the replacement value is stored in a variable.
I tested my command on this file content (filename data.txt):
"groups": [{"group": "MOVIES.ARE.GOOD”}], "groups": [{"group": "no_one.ok”}], "groups": [{"group": "evry_one.lol”}], "groups": [{"group": "NO_VALUES_ARE-GOOD”}], "groups": [{"group": "___scripts”}],
This works for me:
newword=TATA
sed -i "s/\(group\": \"\).*/\1$newword\"\}\],/" data.txt
Basically, everything that follows group": "
is replaced by TATA"}],
My result file now contains:
"groups": [{"group": "TATA"}],
"groups": [{"group": "TATA"}],
"groups": [{"group": "TATA"}],
"groups": [{"group": "TATA"}],
"groups": [{"group": "TATA"}],
Answered By - Nic3500 Answer Checked By - Cary Denson (WPSolving Admin)