How to change Windows line-ending to Unix version

Solution 1:

Option 1: dos2unix

You can use the program dos2unix, which is specifically designed for this:

dos2unix file.txt

will replace all CR from all lines, in place operation.

To save the output in a different file:

dos2unix -n file.txt output.txt

You might need to install it first by:

sudo apt-get install dos2unix

Option 2: sed

Or you can use sed to replace all CR (\r) from line endings:

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

With option -i, the file will be edited in-place, and the original file will be backed up as file.txt.bak.

Solution 2:

The sed solution is not portable to all platforms. It didn't work for me on macOS unless I did brew install gsed and used gsed 's/\r$//'.

For a solution that works in most places without installing anything, I use

tr -d '\r'

To edit a file in-place, I produce the new data in a subshell before erasing and overwriting the original file:

echo "$(tr -d '\r' < file)" > file