-1

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

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
pravin kumar
  • 33
  • 1
  • 7
  • See https://stackoverflow.com/questions/45464280/, https://stackoverflow.com/q/50265987 and https://stackoverflow.com/questions/65090642/, etc. – Wiktor Stribiżew Apr 08 '22 at 07:17
  • One-liner: `v="4dfv45ffdf"; echo '"iso_checksum": "md5:32fdf4fef4ef"' | sed "s/\"md5:[^\"]*\"/\"md5:$v\"/g"` – SergA Apr 08 '22 at 07:26
  • Does this answer your question? [Need sed command to replace JSON property value](https://stackoverflow.com/questions/65090642/need-sed-command-to-replace-json-property-value) – Wiktor Stribiżew Apr 08 '22 at 07:41
  • Below command solved my purpose Thanks Wiktor Stribiżew sed "/\"name\":/s/\(^[^:]*[:][ ]\).*$/\1\"$username\",/" – pravin kumar Apr 08 '22 at 09:28

2 Answers2

1

Correct answer as below

y="4dfv45ffdf"
sed "/\"iso_checksum\":/s/\(^[^:]*[:][ ]\).*$/\1\"md5:$y\",/" file.json
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
pravin kumar
  • 33
  • 1
  • 7
0

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.

Daweo
  • 31,313
  • 3
  • 12
  • 25