Content of a json file
"iso_checksum": "md5:32fdf4fef4ef"
I have stored value of new checksum in a variable v = "4dfv45ffdf"
I want to replace the value after md5: from 32fdf4fef4ef to 4dfv45ffdf after replace above line in the file should like
"iso_checksum": "md5:4dfv45ffdf"
32fdf4fef4ef is not fixed value so we can not just replace like below
sed -i 's/32fdf4fef4ef/4dfv45ffdf/' file
4dfv45ffdf this is also not fixed value so kept in as $v
Can any please help me to perform the above task
Correct answer as below
y="4dfv45ffdf"
sed "/\"iso_checksum\":/s/\(^[^:]*[:][ ]\).*$/\1\"md5:$y\",/" file.json
I would use GNU AWK
for this task following way
awk -v v="4dfv45ffdf" '{gsub("md5:[[:xdigit:]]+","md5:"v);print}' file.json
Explanation: replace every md5:
followed by 1 or more base16-digits using md5:
concatenated with value of v
, print
whole line.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.