简体   繁体   English

如何将file1的每一列附加到file2的特定字段并创建一个新的输出文件?

[英]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. 我想将文件1的每一列作为文件2的第4列附加并导出为新文件,其中包含来自文件1的列号或类似于输出名称的列号。

Input File 1 and 2 have the same number of rows: 输入文件1和2具有相同的行数:

Input File 1 has N columns: 输入文件1有N列:

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 输入文件2最初有5列

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: 例如,前3个输出文件如下所示:

Output File 1 (column 1): 输出文件1(第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): 输出文件2(第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): 输出文件3(第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. 新文件名可以是file1,file2,file3 ...或column1,column2,column3 ....或类似的东西。 How can I achieve this please? 我怎么能实现这个目标呢? (for loop, awk, paste, etc.) (用于循环,awk,粘贴等)

Any suggestions would be appreciated. 任何建议,将不胜感激。

If your columns are tab-separated, you can easily profit from cut and paste : 如果您的列以制表符分隔,则可以轻松地从cutpaste获益:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据与file2的列匹配删除file1中的行 - How to delete lines in file1 based on column match with file2 如果file1的B列= file2的B列,则将A列file1替换为file2的A列 - If column B of file1 = column B of file2, replace column A file1 with column A of file2 如何使用 grep 或 unix 命令将 file1 的列与 file2 的列、select 匹配值以及 output 与新文件进行比较 - How to compare the columns of file1 to the columns of file2, select matching values, and output to new file using grep or unix commands 将文件 1 中的数据追加/补充到文件 2 (linux) - Append/supplement data from file1 to file2 (linux) 当 ID 与 file2 匹配时,从 file1 复制一列,并根据文件 2 打印 output - copy a column from file1 when the ID's matches to file2 and print output according to file 2 如果在file2中找不到file1的输出行 - Output line from file1 if not found in file2 将带有一列的 file1 与来自 file2 的两列进行比较 - Compare file1 with one column to two columns from file2 如何基于文件/ file1(仅)第一列与linux中的file2的匹配信息从file1提取行? - how to extract rows from file1 based on matching information of its/file1 (only)first column with file2 in linux? 如何测试 File2 中是否存在来自 File1 每一行的 substring - How to test if a substring from each line of File1 exists in File2 如何使用awk删除Ubuntu的file2中存在列1值的file1行? - How to use awk to delete lines of file1 whose column 1 values exist in file2 in Ubuntu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM