Awk 如何在多个文件中每隔一列相乘

Awk 如何在多个文件中每隔一列相乘,awk,Awk,全部 我有三个这样的文件:file1file2file3,它们的行数和列数相同。每个文件包含72列(前两列相同,(第2n+1列也相同),如 文件1 文件2 文件3 从第4列开始,我想每隔一列相乘,比如col4,col6,col8,col10。。。输出应该是 20170101 1 1 0 2 6 3 0... 20170101 2 1 4 2 64 3 16... 20170101 3 1 60 2 48 3 0 .. 我尝试了这个方法,但是从col4开始的所有列都将相乘 paste fil

全部

我有三个这样的文件:file1file2file3,它们的行数和列数相同。每个文件包含72列(前两列相同,(第2n+1列也相同),如

文件1

文件2

文件3

从第4列开始,我想每隔一列相乘,比如col4,col6,col8,col10。。。输出应该是

20170101 1 1 0 2 6 3 0...  
20170101 2 1 4 2 64 3 16...
20170101 3 1 60 2 48 3 0 ..
我尝试了这个方法,但是从col4开始的所有列都将相乘

paste file1 file2 file3| awk '{ for(i=3;i<=NF/2; i++) printf("%4.2E ", $i*$(i+NF/2)*$(i+NF))); printf("\n"); }'

粘贴文件1文件2文件3 | awk'{for(i=3;i假设所有输入文件具有相同的行数:

$ awk '{getline a < "f2"; getline b < "f3"; split(a,s1); split(b,s2)}
       {for(i=4;i<=NF;i+=2) $i*=s1[i]*s2[i]; print}' f1
20170101 1 1 0 2 6 3 0
20170101 2 1 4 2 64 3 16
20170101 3 1 60 2 48 3 0
$awk'{getline a<“f2”;getline b<“f3”;拆分(a,s1);拆分(b,s2)}

{for(i=4;i由于您使用的是
paste
命令,我将在回答中使用它,否则我真的建议使用Sundeep的解决方案和
getline

以下“粘贴”命令将交错这3个文件:

$ paste -d '\n' file1 file2 file3
20170101 1 1 1 2 2 3 3 
20170101 1 1 0 2 1 3 3  
20170101 1 1 2 2 3 3 0
20170101 2 1 2 2 4 3 4
20170101 2 1 2 2 4 3 2
20170101 2 1 1 2 4 3 2
20170101 3 1 5 2 3 3 6
20170101 3 1 3 2 4 3 1
20170101 3 1 4 2 4 3 0
在输出上使用
awk
后,您可以使用此输出生成您需要的输出:

$ paste -d '\n' file1 file2 file3 | awk '{for(i=4;i<=NF;i+=2)if(NR>1){a[i]*=$i;}else{a[i]=$i}if(NR%3==0){printf $1 OFS $2 OFS; for(i in a){printf $(i-1) OFS a[i] OFS;a[i]=1}printf ORS;}}'
20170101 1 1 0 2 6 3 0 
20170101 2 1 4 2 64 3 16 
20170101 3 1 60 2 48 3 0 
$paste-d'\n'file1 file2 file3 | awk'{for(i=4;i1){a[i]*=i;}否则{a[i]=i}如果(NR%3==0){printf$1 of s$2 of s;对于(i in a){printf$(i-1)of s[i]of s;a[i]=1}printf ORS;}
20170101 1 1 0 2 6 3 0 
20170101 2 1 4 2 64 3 16 
20170101 3 1 60 2 48 3 0 
解释:

{
    for(i=4;i<=NF;i+=2) #loop on all other columns 
        if(NR>1){a[i]*=$i;} #make the product computation for all lines greater than 1
        else{a[i]=$i} #initialize the array on the first line
    if(NR%3==0){ #every 3 lines 
        printf $1 OFS $2 OFS; #print the first 3 fields
        for(i in a){ #then loop on the array and print cell and computed product
            printf $(i-1) OFS a[i] OFS;
            a[i]=1} #reset all elements of the array to 1
        printf ORS;} #print EOL
}
{
对于(i=4;i1){a[i]*=$i;}#对大于1的所有行进行乘积计算
else{a[i]=$i}初始化第一行上的数组
如果(NR%3==0){#每3行
printf$1 OFS$2 OFS;#打印前3个字段
对于a{#中的(i),则在数组上循环并打印单元格和计算乘积
打印F$(i-1)OFS a[i]OFS;
a[i]=1}#将数组的所有元素重置为1
printf ORS;}#打印下线
}

您的解决方案比我的+1!;-)给新来者的建议:如果答案解决了您的问题,请单击大复选标记接受它(✓) 在它旁边,也可以选择向上投票(向上投票要求至少15个信誉点)。如果您发现其他答案有帮助,请向上投票。接受和向上投票有助于未来的读者。请参阅[相关帮助中心文章][1][1]:
$ paste -d '\n' file1 file2 file3
20170101 1 1 1 2 2 3 3 
20170101 1 1 0 2 1 3 3  
20170101 1 1 2 2 3 3 0
20170101 2 1 2 2 4 3 4
20170101 2 1 2 2 4 3 2
20170101 2 1 1 2 4 3 2
20170101 3 1 5 2 3 3 6
20170101 3 1 3 2 4 3 1
20170101 3 1 4 2 4 3 0
$ paste -d '\n' file1 file2 file3 | awk '{for(i=4;i<=NF;i+=2)if(NR>1){a[i]*=$i;}else{a[i]=$i}if(NR%3==0){printf $1 OFS $2 OFS; for(i in a){printf $(i-1) OFS a[i] OFS;a[i]=1}printf ORS;}}'
20170101 1 1 0 2 6 3 0 
20170101 2 1 4 2 64 3 16 
20170101 3 1 60 2 48 3 0 
{
    for(i=4;i<=NF;i+=2) #loop on all other columns 
        if(NR>1){a[i]*=$i;} #make the product computation for all lines greater than 1
        else{a[i]=$i} #initialize the array on the first line
    if(NR%3==0){ #every 3 lines 
        printf $1 OFS $2 OFS; #print the first 3 fields
        for(i in a){ #then loop on the array and print cell and computed product
            printf $(i-1) OFS a[i] OFS;
            a[i]=1} #reset all elements of the array to 1
        printf ORS;} #print EOL
}