简体   繁体   中英

How to append each column of file1 to a specific field of file2 and make a new output file?

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.

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

This method processes each file only once, which is a help if the files are large. It does, however, require the first file to be stored in memory:

awk '
    NR==1 {n=NF} 
    NR==FNR {
        for (i=1; i<=n; i++) 
            file1[i, FNR]=$i
        next
    }
    {
        for (i=1; i<=n; i++) {
            filename = "merged" i
            print $1, $2, $3, file1[i, FNR], $4, $5 >> filename
        }
    }
' file1 file2

Something like this is all you need:

awk '
NR==FNR { hd=$1" "$2" "$3"; tl=$4" "$5; next }
{  for (i=1;i<=NF;i++) {
      print hd, $i, tl > "file" i
   }
}
' file2 file1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM