Why does the paste command add line breaks?

10,279

The file which was the source for t was created using notepad on Windows 8 and copied by Ubuntu 13.04 into my home directory. The source for d was created on Ubuntu in gedit. Thus, the carriage returns were in the file all along. It seems that moving files back and forth between different operating systems results in problems like this fairly often.

newline differences

converting line endings

Share:
10,279
Lee
Author by

Lee

Updated on September 18, 2022

Comments

  • Lee
    Lee over 1 year

    I have 2 files: d and t. I would like to be able to combine these files so that the first line of file t is followed by a tab and then the first line of d. For shorter lines, paste t d seems to work fine.

    $ cat d t
    Highly reactive metals in group 1A of the periodic table.
    Fairly reactive metals in group 2A of the periodic table.
    alkali metals
    alkaline earth metals
    $ paste d t
    Highly reactive metals in group 1A of the periodic table.   alkali metals
    Fairly reactive metals in group 2A of the periodic table.   alkaline earth metals
    $ paste t d
    alkali mHighly reactive metals in group 1A of the periodic table.
    alkalineFairly reactive metals in group 2A of the periodic table.
    

    Trying to paste full sentences seems to act strangely. As seen above, the terms get trimmed down to the first 8 characters.

    $ paste t d > temp
    $ gedit temp &
    $ vim temp
    

    Opening gedit shows line breaks following each term. Vim shows this:

    alkali metals^M Highly reactive metals in group 1A of the periodic table.
    alkaline earth metals^M Fairly reactive metals in group 2A of the periodic table.
    

    Well, that seems to be easy enough to fix. :%s/^V^M//g removes all of the carriage returns, and everything shows up correctly. But how did those carriage returns end up there in the first place?

    While my question does involve carriage returns in a text file from Windows making things act strangely in a Unix-like environment, it is not a duplicate of this question. The problems are similar, but the manifestations are very different. It took me around an hour to figure out that carriage returns were even the culprit because I could not find an instance of a similar enough problem through web searches. That is why I posted this after having solved it myself.

    • Lee
      Lee over 10 years
      The file which was the source for t was created using notepad on Windows 8 and copied by Ubuntu 13.04 into my home directory. The source for d was created on Ubuntu in gedit. Thus, the carriage returns were in the file all along. It seems that moving files back and forth between different operating systems results in problems like this fairly often. newline differences converting line endings
    • Jeff Hewitt
      Jeff Hewitt over 10 years
      In such cases, you should use dos2unix to sanitize the Notepad-tainted files before processing.
    • Lee
      Lee over 10 years
      That is the first thing mentioned in the answer to the question I linked to on converting line endings.
    • Jeff Hewitt
      Jeff Hewitt over 10 years
      Sorry, didn't follow the link; I thought it was simply an explanation for newline differences (didn't know they were 2 links).
    • Lee
      Lee over 10 years
      I was originally going to answer my own question and had the links on two separate lines. Evidently I can't go back and edit that comment. I'll learn this system yet!