-1

I have a text file say file.txt containing following data:

APP_ARTIFACT=xxxx
APP_DOMAIN=xxx
APP_NAME=xxx
APP_OWNER_EMAIL=xxx
APP_PATH=xxx
IMAGE=dockerhub.ccc.xx.net:5005/aaa/xxx-rhel7:2.4
NAMESPACE=xxx
PAAS=xxxnce1

Now, I want to write a script in .sh which can replace the version after 'IMAGE=dockerhub.ccc.xx.net:5005/aaa/xxx-rhel7:' to a different version.

It means i want to replace 2.4 (or it may contain some other string as well) with some other string., while keeping the rest of the file as it is. How can I do this using some inbuilt linux tool like 'sed'?

after replacing the string containg version with 'mystring;, the expected output after substitution should be :

APP_ARTIFACT=xxxx
APP_DOMAIN=xxx
APP_NAME=xxx
APP_OWNER_EMAIL=xxx
APP_PATH=xxx
IMAGE=dockerhub.ccc.xx.net:5005/aaa/xxx-rhel7:mystring
NAMESPACE=xxx
PAAS=xxxnce1
shellter
  • 36,525
  • 7
  • 83
  • 90
Harshit Goel
  • 175
  • 1
  • 3
  • 13
  • why not `sed 's/dockerhub.ccc.xx.net:5005\/aaa\/xxx-rhel7:.*/dockerhub.ccc.xx.net:5005\/aaa\/xxx-rhel7:mystring/` ? – January Jul 11 '19 at 15:07

1 Answers1

1

Assuming you want the new version to be 555, you could do:

sed -re 's/(^IMAGE.*):(.*)/\1:555/'

The above matches the line starting with IMAGE and looks at everything after it. It makes sure you have a colon (:) and uses the first match (\1) and appends a colon 555 (:555) for your new version.

You could use a variable as well:

myver="555"
sed -re "s/(^IMAGE.*):(.*)/\1:$myver/"
somelement
  • 126
  • 7
  • sed -re 's/(^IMAGE.*):(.*)/\1:555/' is working fine, but the second one is printing $myver itself (and not 555). – Harshit Goel Jul 11 '19 at 15:39
  • Also: I am using " sed -re 's/(^IMAGE.*):(.*)/\1:555/' file.txt ". The value file is printing on console but the file is not updating. I tried using -i and -ire also in place of -re only. – Harshit Goel Jul 11 '19 at 15:43
  • Did you have a problem adding `-i` to your cmd-line? If so, then you are probably working on a mac, where the solution is `-i ''`. Search here for a 1000 explanations about why. Good luck. – shellter Jul 11 '19 at 16:44
  • Note the single quote ' versus the double quote " in the example. You need to use double quote " if you want $myver to expand. Agreed with @shellter - the default Mac OS ships with a version of sed that doesn't act the same as the GNU sed. [Here's one example](https://stackoverflow.com/questions/4247068/sed-command-with-i-option-failing-on-mac-but-works-on-linux) – somelement Jul 12 '19 at 11:21