我有10个文件具有相同的制表符分隔列结构。我需要将每个文件中的第8列和第9列合并。我想出了以下AWK代码,但它一次只合并了两个文件。我正在寻求帮助合并所有10个文件同时,我不确定这是否可行。
我所有的文件名都遵循相同的模式(s1s2.txt,s3s4.txt,s5s6.txt,.)(s 19s 20.txt)
#!/bin/bash
awk '
BEGIN {
#load array with contents of the first file
while ( getline < "s1s2.txt" > 0)
{
s1s2_counter++
f1_8[s1s2_counter] = $8
f1_9[s1s2_counter] = $9
}
}
{OFS="\t"}
{ #output the columns 8 and 9 from the first file before the second file
print f1_8[NR],f1_9[NR], $8, $9
} ' s3s4.txt发布于 2013-09-23 19:32:23
awk -F'\t' '{a[FNR] = a[FNR] (NR==FNR?"":FS) $8 FS $9} END{for (i=1;i<=FNR;i++) print a[i]}' s1s2.txt .... s19s20.txt使用getline通常是错误的方法,请参阅http://awk.info/?tip/getline。
https://stackoverflow.com/questions/18967202
复制相似问题