- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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
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
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.
- How to avoid printing newline in Python?
- How to print without newline in Python?
- Print newline in PHP in single quotes
- Difference between DOS and Windows
- Putting a newline in Matplotlib label with TeX in Python
- How to split on successions of newline characters using Python regular expression?
- Remove newline, space and tab characters from a string in Java
- How to match tab and newline but not space using Python regular expression?
- Read a character from standard input without waiting for a newline in C++
- System Calls in Unix and Windows
- How to convert MySQL datetime to Unix timestamp?
- How to convert Python date to Unix timestamp?