Ad

How To Append Each Column Of File1 To A Specific Field Of File2 And Make A New Output File?

- 1 answer

I want to append each column of file 1 as the 4th column of file 2 and export as a new file with the column number from file 1 or something similar as the output name.

Input File 1 and 2 have the same number of rows:

Input File 1 has N columns:

12 23 34  .....
33 34 23
67 09 34
45 67 34
65 76 44
64 33 96

Input File 2 originally has 5 columns

AA BB FF DD 6
AA CC HH NN 7
AA DD II RR 4
AA EE JJ PP 2
AA FF KK QQ 9
AA GG LL SS 8

For example, the first 3 output files would look like this:

Output File 1 (column 1):

AA BB FF 12 DD 6
AA CC HH 33 NN 7
AA DD II 67 RR 4
AA EE JJ 45 PP 2
AA FF KK 65 QQ 9
AA GG LL 64 SS 8

Output File 2 (column 2):

AA BB FF 23 DD 6
AA CC HH 34 NN 7
AA DD II 09 RR 4
AA EE JJ 67 PP 2
AA FF KK 76 QQ 9
AA GG LL 33 SS 8

Output File 3 (column 3):

AA BB FF 34 DD 6
AA CC HH 23 NN 7
AA DD II 34 RR 4
AA EE JJ 34 PP 2
AA FF KK 44 QQ 9
AA GG LL 96 SS 8

The new file names can be file1, file2, file3...or column1, column2, column3....or something similar. How can I achieve this please? (for loop, awk, paste, etc.)

Any suggestions would be appreciated.

Ad

Answer

If your columns are tab-separated, you can easily profit from cut and paste:

for i in {1..N} ; do  # Insert the real N here, or change to $(seq 1 $N)
    cut -f1-3 input2 | \
        paste - \
              <(cut -f$i input1) \
              <(cut -f4- input2) \
        > output$i
done
Ad
source: stackoverflow.com
Ad