Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

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.

share|improve this question

marked as duplicate by Gilles, Renan, slm, Anthon, rahmu Sep 10 '13 at 7:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
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 –  Lee Sep 7 '13 at 18:37
1  
In such cases, you should use dos2unix to sanitize the Notepad-tainted files before processing. –  Joseph R. Sep 7 '13 at 19:47
    
That is the first thing mentioned in the answer to the question I linked to on converting line endings. –  Lee Sep 7 '13 at 20:28
    
Sorry, didn't follow the link; I thought it was simply an explanation for newline differences (didn't know they were 2 links). –  Joseph R. Sep 7 '13 at 20:31
1  
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! –  Lee Sep 7 '13 at 20:35

1 Answer 1

up vote 2 down vote accepted

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|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.