Removing Carriage return on Mac OS X using sed

It is because sed available on OSX doesn't recognize \r as a special character unlike the sed on Linux does.

You can either use it the way you're using:

sed -i.bak $'s/\r//' file

OR this:

sed -i.bak "s/$(printf '\r')//" file

OR else you can use tr on OSX:

tr -d '\r' < file

Another portable and flexible solution is:

sed -i.bak $'s/\x0D//' file

Because the return character has the ASCII code 0D. \x substitutions work for all POSIX releases of sed, and you can match any other troublesome character by looking up the ASCII code. To find other valid substitutions, execute man re_format or view an ASCII Table.

The final /g is needed for Linux because the carriage return (\r) does not end the line. Many Windows "plain text" editors (e.g. Notepad) end each line with both carriage return and new line (\r\n), but Macs with OS 9 (ca. 2001) or earlier ended each line of a "plain text" file with a single \r. If you're cleaning up a Windows file, the /g is not needed on any *X system. If you're on macOS, the /g isn't needed, either, because macOS recognizes the single \r as a line ending.

(A Linux system reading an old Mac file will think that all the text is on one very long line and only convert the first \r. If you're on a Linux system and need to preserve the line breaks from an old Mac file,

sed -i.bak $'s/\x0D/\x0A/g' file

converts each \r to \n.)