converting the ^M character back to newline on linux and mac
Note:
This is an export from my old wordpress website, your comments or references might be lost during the migration. Sorry for the inconvenience.
Sometimes when I saved a sheet from Excel on a Windows or Mac OSX computer as a tab-delimited file for further data processing, I frequently encountered the annoying ‘^M’ issues. To save my own time and to help the others who have the same issues, here are the solutions:
Open the file you want to convert with vim
, then replace the ‘^M’ characters to \r
like this:
:%s/CTRL-V+CTRL-M/\r/g
:wq
Here CTRL-V+CTRL-M
really means hold your Control-V and Control-M keys, which will be finally shown as ‘^M’ in place.
Alternatively, you can also simply use the dos2unix
command:
# for linux
sudo apt install dos2unix
dos2unix input_file1 [input_file2 ... input_filen]
# for macOS
brew install dos2unix
dos2unix -c mac input_file1 [input_file2 ... input_filen]