0

I have a json file with a line as below,

"image": "docker.abc.com/abc-pr0000abc:ABCCoreUI-46",

Can some one help me with sed command that can replace above as below,

"image": "docker.abc.com/abc-pr0000abc:ABCCoreUI-47",

Also I would like to wild card 46, something like ABCCoreUI-*

I tried sed s/ABCCoreUI-4*/ABCCoreUI-47/ file.json

Output become,

"image": "docker.abc.com/abc-pr0000abc:ABCCoreUI-4742",

Thanks

  • Are the xxxs static obfuscated strings, or are they different in every instance you wish to alter? – DopeGhoti May 9 '17 at 16:54
  • thats correct. xxx is always static. – user2068179 May 9 '17 at 17:00
  • in regex (used by sed) * means any 0 or more of previous atom (character or bracketed thing). You are mixing it up with globs. Just replacing your * with .* should do it. ( . means any character [.] or \. means . ). – ctrl-alt-delor May 9 '17 at 18:07
1

Also I would like to wild card 46, something like ABCCoreUI-*

I think no one answered what you asked "FIX your pattern to match any digit at the end of your string, maybe due to behavior of * character at last position, output is concatenating 47+42"

You may wrap the regexp in '' to prevent Shell Globs

Then, to match zero or more digits

sed -r 's/ABCCoreUI-[0-9]*/ABCCoreUI-47/' file.json
sed -r 's/ABCCoreUI-[[:digit:]]*/ABCCoreUI-47/' file.json

To match zero or more characters

sed -r 's/ABCCoreUI-.*/ABCCoreUI-47/' file.json
  • -r is to enable extended sed regexp
  • [0-9] or [[:digit:]] (POSIX) to match a digit
  • [0-9]* or [[:digit:]]* to match zero or more digits
  • .* to match zero or more characters

Note that \d is not working.

  • + requires extended regular expressions. You'd need an option like -E for that. And \d is unknown to most sed implementations; use [0-9] instead. – Philippos May 10 '17 at 10:49
0

This quick-and-dirty sed appears to do the trick:

sed '/image.*docker\.xxx\.com.*CoreUI-46/s/CoreUI-46/CoreUI-47/' /path/to/yourfile.json
  • The OP not only wants to replace 46, but any number [0-9]* – Philippos May 10 '17 at 10:51
  • This was not made clear in the version of the question which I answered. Better answers have been provided in the interim since the clarification. – DopeGhoti May 10 '17 at 15:58
0

In most simple case you can apply the following sed approach:

sed 's/\("image": "[^"]*-\)46"/\147"/g' jsonfile
0

Below worked for me

sed "s|\(^.*\"image\": \"docker.abc.com/abc-pr0000abc:\).*|\ABCCoreUI-47\",|g" -i artifactorytest.json

  • (1) I guess you forgot \1 in the replacement string. (2) ^ is not neccessary if you start with .*. (3) If you use single quotes for the script, you don't need to escape all double quotes. (4) We don't know whether the can be matches with other strings than `ABCCoreUI' that should be left alone. – Philippos May 10 '17 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.