--- title: "M4 as a replacement for sed" date: 2019-03-15T00:00:00+02:00 --- Writing a tutorial often involves to replace a placeholder in a file, such as: *Replace FOO with the actual name of your image:* ```sh sed 's|IMAGE_NAME|docker.io/foo/bar:latest|g' template.yaml |kubectl apply -f - ``` But this approach has several drawbacks: - If you have to replace multiple placeholders, the sed syntax becomes cumbersome. - If the delimiter appears in your replacement string, you will have to find another delimiter (such as in the previous example where the usual slash has been replaced by a pipe to accomodate the slash in the image name). - The `sed` command has some subtleties between the GNU (any Linux distribution) and the BSD (MacOS) flavors. For this specific use case (replacing placeholders), I would like to introduce another tool: the `m4` command. The `m4` command is used in the C/C++ compilation chain to replace pre-processor directives with their actual values. Its syntax is very simple and it is present on most Linux distributions and on MacOS by default. Let's have a look at a very simple example: ```raw $ cat > example <