1 min read

How to preserve backslashes when using sed command

How to preserve backslashes when using sed command

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: