Wednesday, September 06, 2006

remove carriage returns from Windows files on Linux

Sometimes you have to work with a file created in Windows from Linux. The file has the "\r" (0x0d) that can cause trouble for some Linux apps.

You can easily remove them using "tr" cmd.

  • cat windows_file | tr -d "\r"

  • The above command should write the new file to stdout. Save it to a new file, and this will not have "\r" characters.

    What problems may happen
    The "\r" character is generally interpreted by terminals to move the cursor to the beggining of the current line. The "\n" character moves the cursor to the next line. Generally, a text file created in Windows will have a "\r\n" sequence at the end of each line.

    What would happen if you were to remove the last character from each line and try to display it to the terminal? Now after each line, the cursor would advance to the beginning of the line ("\r") but not advance to the next line. Thus each line will overwrite the other.

    This could happen if you have a perl script where 'chomp' is used to remove the last character of each line.