Blog

Quick tip: get rid of pesky Windows newlines from the Linux command line

Today I used the Windows Notepad to save a text file onto my Debian server, thinking that I'd be able to view it easily using vi. But when I tried to do so, every line ended with an "^M". Those "^M"s are newline characters. Windows and Linux use different newline characters, so in order to have a Windows text document display properly in Linux, some conversion is needed. I used the handy tr command like so:

0 jack@srv-01:~$ cat text_windows.txt | tr -d \\r > text_unix.txt

And now the "^M"s are gone! Of course, text_unix.txt now looks funky in Windows, but for my purposes that's just fine; you could always save a Windows version and a Linux version if you'll need to view your file from both operating systems.

Comments

You can streamline this with

sudo aptitude install tofrodos

and then

dos2unix text_windows.txt

and

unix2dos text_windows.txt

(if necessary) – the benefit being you don’t need to remember the current newline characters in the file, the tools simply won’t alter the file if its in the target newline standard already.

Post new comment

(If you're a human, don't change the following field)
Your first name.
The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.