How to preserve backslashes when using sed command
![How to preserve backslashes when using sed command](/content/images/size/w960/2024/05/photo-1526566661780-1a67ea3c863e.jpeg)
Today I faced an interesting task. I wanted to generate a bunch of yaml files with different names to test some payloads. Each time I generated a yaml file, I needed to uploaded the file to the system manually, check the result and then move to the next generated file.
So this is the yaml template:
And the file with payloads, say:
The first thought is to use sed
to replace the replaceme
string with the payload. So potential script would look like so:
The problem with the script is that sed
considers backslashes (\
) as an escape symbol, so the resulting yaml for the first payload will look like
Solution
What would be the solution in this case? We can use the printf
command to workaround this situation:
which will produce the correct yaml:
Member discussion