1

I am a novice programmer. I am using unix's join command to self-join couple long files together.

join -j30 test test2
col1 col2 ... col30    col1 col2 ... col30
A    B        ZZZ   ^M A    B        ZZZ

I am getting this ^M character in my output.

Why is it there? and How would I remove it?

EDIT: Below is a screenshot of my part of my output

enter image description here

2
  • Are you sure it is join and not that one of the input files has CR in it?
    – thrig
    Commented Apr 18, 2017 at 19:31
  • How would I go about checking if it has a CR (assuming that means Carriage Return; excuse my lack of knowledge)? There are \n characters in the original file Commented Apr 18, 2017 at 19:34

2 Answers 2

3

The ^M means you are bringing over/editing file in Windows.

Use the dos2unix command over the files to convert them to Unix text mode.

DOS uses carriage return and line feed "\r\n" as a line ending, while Unix uses just line feed "\n". The ^M are a visual representation of the "extra" \r characters.

To install the dos2unix command, do (on Debian-based distros):

sudo apt-get install dos2unix

or in a Mac (MacPorts):

sudo port install dos2unix

Alternatively, you can also do it with sed as in:

sed 's/\r$//' dosfile.txt > unixfile.txt
8
  • Does the dos2unix command come standard to unix machines? I am getting command not found Commented Apr 18, 2017 at 19:39
  • added to the answer. Commented Apr 18, 2017 at 19:41
  • I am on a MacOS and can use apt-get. I used your sed command and it does not remove the ^M characters. Commented Apr 18, 2017 at 19:43
  • @NicholasHayden and now this version of sed? Have you Macports, fink, or anything similar? Commented Apr 18, 2017 at 19:51
  • Unfortunately, I don't. The only package manger I have is conda but that more for python. But I think the tr option may work. I am playing around with it in combination with od -bc to see if it is removing. But the problem is \r\n characters from the original file. Commented Apr 18, 2017 at 19:54
1

join(1) (at least the version I'm using) does not add ^M or carriage returns to joined files; join will however happily copy ^M along. A hex viewer plus some test cases can show what is going on:

$ echo "1 a" > test1
$ echo "1 b" > test2
$ join test1 test2 | od -bc
0000000   061 040 141 040 142 012                                        
           1       a       b  \n                                        
0000006
$

And a test case where there are ^M in the input files:

$ perl -E "say qq(1 a\r)" > test1
$ perl -E "say qq(1 b\r)" > test2
$ join test1 test2 | od -bc
0000000   061 040 141 015 040 142 015 012                                
           1       a  \r       b  \r  \n                                
0000010
$ 

There are various ways to correct the input files, besides dos2unix one could also use tr (though this would be a terrible idea if the file uses the ancient MacOS tradition of \r as a line delimiter):

$ od -bc test1
0000000   061 040 141 015 012                                            
           1       a  \r  \n                                            
0000005
$ tr -d '\r' < test1 > foo
$ mv foo test1
$ od -bc test1            
0000000   061 040 141 012                                                
           1       a  \n                                                
0000004
$ 
1
  • I did not propose tr as sed is a bit more cleaner, defining only to delete the carriage return at the end of the file. Commented Apr 18, 2017 at 19:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .