0

I need to paste two columns (in a file) to a very big file with an identical number of columns (length 48554). I have tried this

I have two files with columns separated by tabs. File 1 looks like this:

Header_1 header_2

0 23

1 25

and file 2 looks like this:

Header_3 header_4

2 24

3 26

What I want is this:

Header_1 header_2 Header_3 header_4

0 23 2 24

1 25 3 26

I have tried paste, e.g. this:

paste file1 file2 | pr -t -e24

  • but I get this:
Header_1 header_2

0 23

1 25 Header_3

header_4

2 24

3 26
  • i.e, the problem is that paste appends the new columns in file 2 to the bottom of the last column in file 1, not added side by side as two extra columns in the +5000 column matrix as I need.

    What am I doing wrong?

0

Although your delimeter looks more like a space than a tab, paste works fine for me.

Alternatives you could try

join <(nl file1) <(nl file2) | cut -d' ' -f2-5

or

awk '{ if (FNR==NR) {F[NR]=$1;G[NR]=$2; next}; {printf "%s\t%s\t%s\t%s\n", $1, $2, F[FNR], G[FNR]}}' file2 file1
  • Thanks for the answer, but unfortunately none of these suggestions work. My delimiter in the actual files is indeed a tab, though it may be reproduced as a space here, but either way, the paste command returns identically useless output. – Christoffer Bugge Harder Oct 27 at 22:59
  • Your suggestions here: > join <(nl file1) <(nl file2) | cut -d' ' -f2-5 gives this output: >Header_1 Header_2 >0 23 Your AWK suggestion gives this output: >Header_1 Header_2 Header_3 Header_4 >2 I made the files in excel, and saved them as tab-separated txt files - I have verified the hidden codes and tabular separations. It´s simply that nothing is working. :( – Christoffer Bugge Harder Oct 27 at 23:06
  • How does your paste oneliner look? No matter how I try it, it either does nothing at all, or merges the top of the columns in file 2 to the bottom of columns in file1. – Christoffer Bugge Harder Oct 27 at 23:08
  • just like this paste file1 file2 | pr -t s24 although the pr doesn't seem to do anything on my system...... – bu5hman Oct 28 at 16:49
0

I can actually make it work in R by importing both files as tables/data frames:

 file1<-read.table("file1.txt") 
 file2<-read.table("file2.txt")

and then using cbind simply

combined.txt <- cbind(file1, file2)

but I struggle to comprehend that there could be no simple unix feature that can accomplish the same that worked for me........

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.