I have an XML file containing a path which is different depending of my environments. I created a $ENV_PATH variable which contain the correct string. How can I parse and replace $ENV_PATH
in my XML file by the value of the environment variable?
Asked
Active
Viewed 1,461 times
0
-
using [subst](https://www.systutorials.com/docs/linux/man/n-subst/) ? – mpromonet Nov 16 '21 at 21:05
-
Can you give an example of what you mean? How does the value appear in your XML and how do you want it to appear after replacement? `sed` is a common Unix tool for replacing strings and doing other edits to files. I would recommend that. I'm not sure this is _really_ a GitLab question though. – sytech Nov 17 '21 at 01:27
-
Does this answer your question? [Use the contents of a file to replace a string using SED](https://stackoverflow.com/questions/6790631/use-the-contents-of-a-file-to-replace-a-string-using-sed) – sytech Nov 17 '21 at 03:51
1 Answers
1
Assuming you're running on linux, you can use the sed
command to do exactly what you're asking. If you have the following XML file in my_file.xml
:
<example>
<my_tag>SUBSTITUTE_ME</my_tag>
</example>
You can use the following command:
sed -i 's/SUBSTITUTE_ME/$ENV_PATH/g' my_file.xml
This will result in the following file (if ENV_PATH=/dev)
<example>
<my_tag>/dev</my_tag>
</example>

Patrick
- 2,885
- 1
- 14
- 20