1

I was trying to merge files using either of these commands:

paste data_{1..2}.txt > 1_2.txt

paste <(awk '{print $1}' data_2.txt ) <(awk '{print $1}' data_2.txt) > 1_2.txt

But the columns in the output don't line up. I tried to clean tabs and spaces but I still get the same result. Here is my sample data below for data_1.txt:

TMAX
34.2481
19.1582
-0.903817
-0.408851
-0.849964
0.596377
0.922126
-5.13179
-17.4449
-15.1031
-12.5849
-12.2548

...and for data_2.txt:

TMAX
33.629
18.5924
-1.37411
-1.00714
-1.48201
0.0046866
0.295162
-5.69127
-18.0672
-15.7163
-13.1048
-12.8443
-11.9689

The combined output:

TMAX
    TMAX
34.2481
    33.629
19.1582
    18.5924
-0.903817
    -1.37411
-0.408851
    -1.00714
-0.849964
    -1.48201
0.596377
    0.0046866
0.922126
    0.295162
-5.13179
    -5.69127
-17.4449
    -18.0672
-15.1031
    -15.7163

What is expected of course is for the columns to line up like so:

TMAX    TMAX
34.2481 33.629
19.1582 18.5924
-0.903817   -1.37411
-0.408851   -1.00714
-0.849964   -1.48201
0.596377    0.0046866
0.922126    0.295162
-5.13179    -5.69127
-17.4449    -18.0672
-15.1031    -15.7163
-12.5849    -13.1048
-12.2548    -12.8443
-11.371 -11.9689

Why am I getting the distorted output with lines split and indentation?

5
  • 2
    Does cat -et data_1.txt show any weird output? Commented Sep 2, 2014 at 20:35
  • Yes it does. Here is the weird output TMAX^M$ 34.2481^M$ 19.1582^M$ -0.903817^M$ -0.408851^M$ -0.849964^M$ 0.596377^M$ 0.922126^M$ -5.13179^M$ -17.4449^M$ -15.1031^M$ -12.5849^M$ -12.2548^M$ -11.371^M$ -7.87503^M$ -13.6309^M$ -21.1465^M$ Commented Sep 2, 2014 at 20:51
  • 3
    Your files ends with \r\n instead of \n. Just remove \r and try paste again. Commented Sep 2, 2014 at 20:54
  • @Gnouc, Thank you for pointing out my problem. I removed them using dos2unix data_2.txt Commented Sep 2, 2014 at 20:59
  • The title could be edited to better describe the problem for people who are later Googling for similar things, but I can't think of a better one myself... Ideas? Commented Sep 2, 2014 at 21:35

1 Answer 1

2

It seems that your files contain some special characters, like carriage return \r\n. You can check using:

cat -et file

If has, you should remove \r before using paste:

tr -d '\r'

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.