sed fails with “unknown option to `s'” error

The problem is with slashes: your variable contains them and the final command will be something like sed “s/string/path/to/something/g”, containing way too many slashes. Since sed can take any char as delimiter (without having to declare the new delimiter), you can try using another one that doesn’t appear in your replacement string: Note that this … Read more

the ‘d’ command in the sed utility

It means that sed will read the next line and start processing it. Your test script doesn’t do what you think. It matches the empty lines and applies the delete command to them. They don’t appear, so the print statements don’t get applied to the empty lines. The two print commands aren’t connected to the … Read more

How can I replace a newline (\n) using sed?

Use this solution with GNU sed: This will read the whole file in a loop (‘:a;N;$!ba), then replaces the newline(s) with a space (s/\n/ /g). Additional substitutions can be simply appended if needed. Explanation: sed starts by reading the first line excluding the newline into the pattern space. Create a label via :a. Append a newline and next … Read more