RE error: illegal byte sequence on Mac OS X

A sample command that exhibits the symptom: sed ‘s/./@/’ <<<$’\xfc’ fails, because byte 0xfc is not a valid UTF-8 char.Note that, by contrast, GNU sed (Linux, but also installable on macOS) simply passes the invalid byte through, without reporting an error. Using the formerly accepted answer is an option if you don’t mind losing support for your true locale (if you’re on a US system … 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

“sed” command in bash

sed is the Stream EDitor. It can do a whole pile of really cool things, but the most common is text replacement. The s,%,$,g part of the command line is the sed command to execute. The s stands for substitute, the , characters are delimiters (other characters can be used; /, : and @ are popular). The % is the pattern to match (here a literal percent sign) and the $ is the second pattern … Read more