4

I have two files that I am trying to merge, one file is:

linux$ cat temp2
linear_0_A_B linear_0_B_A
103027.244444 102714.177778
103464.311111 102876.266667
103687.422222 103072.711111
103533.244444 102967.733333
103545.066667 102916.933333
103621.555556 103027.511111
104255.776536 103006.256983
103712.178771 102877.139665
103817.555556 103198.488889
103701.422222 103133.200000

And the other file is:

linux$ cat temp
linear_1_A_B linear_1_B_A
118620.444444 109212.355556
108408.488889 105744.444444
108136.311111 105174.933333
108627.688889 105390.044444
108356.577778 105412.888889
108559.204420 105667.933702
108469.392265 105547.314917
109032.044693 105497.698324
108925.866667 105986.222222
107975.733333 105070.000000

I want to paste the columns in temp into temp2, and retain the temp2 file like this:

linux$ paste temp2 temp
linear_0_A_B linear_0_B_A       linear_1_A_B linear_1_B_A
103027.244444 102714.177778     118620.444444 109212.355556
103464.311111 102876.266667     108408.488889 105744.444444
103687.422222 103072.711111     108136.311111 105174.933333
103533.244444 102967.733333     108627.688889 105390.044444
103545.066667 102916.933333     108356.577778 105412.888889
103621.555556 103027.511111     108559.204420 105667.933702
104255.776536 103006.256983     108469.392265 105547.314917
103712.178771 102877.139665     109032.044693 105497.698324
103817.555556 103198.488889     108925.866667 105986.222222
103701.422222 103133.200000     107975.733333 105070.000000

But when I do standard output, and display temp2, the result is not the same.

linux$ paste temp2 temp > temp2
linux$ cat temp2
        linear_1_A_B linear_1_B_A
        118620.444444 109212.355556
        108408.488889 105744.444444
        108136.311111 105174.933333
        108627.688889 105390.044444
        108356.577778 105412.888889
        108559.204420 105667.933702
        108469.392265 105547.314917
        109032.044693 105497.698324
        108925.866667 105986.222222
        107975.733333 105070.000000

How to resolve??

1 Answer 1

5

You wrote in your last block,

linux$ paste temp2 temp > temp2

You cannot do this. (Well you can, but it won't work.) What happens here is that the shell truncates temp2 ready to send output from the paste command. The paste temp2 temp command then runs - but by this stage temp2 is already zero length.

What you can do instead is this, which uses a third file to collect the output and then replaces your temp2 with its content. The && ensures that the content is only replaced if the paste "succeeded", and the rm -f removes the transient temp3 file if the mv wasn't triggered, or failed in some unexpected way.

paste temp2 temp > temp3 && mv -f temp3 temp2
rm -f temp3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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