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.

One Response to “Quick tip: get rid of pesky Windows newlines from the Linux command line

  • 1
    Shot
    September 9th, 2009 07:11

    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.

Leave a Reply