How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script?


When we transfer files between Windows and Unix systems, often we come across with issue relating to the end of line character. This is because the EOL character in windows is not recognized as a EOL character in Unix. SO to fix this issue when a file is transferred from Windows to Unix we need to follow one of the below methods.

Using dos2unix

The dos2unix command is used to convert the EOL character of windows platform to Unix platform. Most of the Unix system comes with this command pre-installed. Below we see how we can convert the file itself or save a copy of the file, when using this command.

# change the file itself.
dos2unix file_name.txt
# Save a copy of the file.
dos2unix -n file_name.txt new_file_name.txt

Using sed

When we transfer the file from windows to Unix platform and open to read it, the end of line character appears as ^M (called control-M). So we use sed command to replace the ^M characters. Here also we can save the edited file as a new file.

sed 's/^M$//' file_name.txt > new_file_name.txt

Using tr

The tr command is used in translating or deleting the characters. In this case we will use it to delete the end of the line character in the file from Windows system, if it appears as â\râ in the unix system when opened.

tr -d '\r' file_name.txt > new_file_name.txt

After the above command executes successfully, we can successfully open the file in Unix system without showing the EO L values from Windows system.

Updated on: 03-Jan-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements