HowTo: UNIX / Linux Convert DOS Newlines CR-LF to Unix/Linux Format

ht‮spt‬://www.lautturi.com
HowTo: UNIX / Linux Convert DOS Newlines CR-LF to Unix/Linux Format

To convert DOS newlines (CR-LF) to Unix/Linux format (LF) in a text file on Unix or Linux, you can use the dos2unix utility.

  1. Install the dos2unix package by running the following command:
sudo apt-get install dos2unix
  1. To convert a file, use the dos2unix command followed by the name of the file. For example:
dos2unix myfile.txt

This will convert the DOS newlines in myfile.txt to Unix/Linux format.

Alternatively, you can use the sed command to convert the newlines in a file. For example:

sed -i 's/\r$//' myfile.txt

This will replace all occurrences of CR (\r) at the end of a line ($) with nothing (//), effectively removing the CR character.

You can also use the tr command to convert the newlines. For example:

tr -d '\r' < myfile.txt > myfile-unix.txt

This will read the myfile.txt file and remove all CR characters, writing the result to a new file called myfile-unix.txt.

Note: These commands will modify the original file. If you want to keep a copy of the original file, you can make a backup before running the conversion commands.

Created Time:2017-10-29 22:08:45  Author:lautturi