用于连接两列并在另一个文件中查找连接值的awk脚本

时间:2018-04-02 17:44:14

标签: linux bash awk scripting

需要你帮助解决这个难题。任何类型的帮助将被欣赏和链接任何文件阅读和学习,并处理这样的情况将是有帮助的

连接文件1的column1和column2。然后检查File2的Column1中的连接值。如果找到提取File2的column2和column3的相应值,则再次连接File2的column1和column2。现在在File1中找到这个连接的值,如果找到了

例如 - 连接File1的column1(262881626)和column2(10)。然后在File2的column1中查找此连接(26288162610)值并提取File2的相应column2和column3值。 现在再次连接File2的column1和column2,并在File1中查找此连接(2628816261050)值,并将连接值(26288162610)提取的汇率(2)乘以与File1的2628816261050对应的应税值(65)。将multiplcation值的结果存储在File1的column4(AD)中

File1

    Bill Doc     LineNo     Taxablevalue     AD
 262881626         10        245
 262881627         10        32
 262881628         20        456
 262881629         30         0
 262881630         40         45
2628816261050      11        65
2628816271060      12        34
2628816282070      13        45
2628816293080      14        0
2628816304090      15           

文件2

Bill.Doc     Item     Exch.Rate     
26288162610     50     2    
26288162710     60     1    
26288162820     70     45
26288162930     80     1    
26288163040     90     5

输出文件

 Bill Doc        LineNo  Taxablevalue   AD
262881626        10       245
262881627        10       32
262881628        20       456
262881629        30       0
262881630        40            
2628816261050     11      65            130
2628816271060     12      34            34
2628816282070     13      45            180
2628816293080     14      0              0
2628816304090     15

1 个答案:

答案 0 :(得分:1)

虽然你的输出不清楚,但是请你试试看,让我知道这对你有所帮助。

awk -F"|" 'FNR==NR{a[$1$2]=$NF;next} {print $0,$1 in a?"|" a[$1]*$NF:""}' OFS=""   File2 File1

<强> 说明:

awk -F"|" '                         ##Setting field separator as |(pipe) here.
FNR==NR{                            ##Checking condition here FNR==NR which will be TRUE when first file named File2 is being read.
  a[$1$2]=$NF;                      ##Creating an array named a whose index is $1$2(first and second field of current line) and value if last field.
  next}                             ##next will skip all further statements from here.
{                                   ##Statements from here will be executed when only 2nd Input_file named File1 is being read.
  print $0,$1 in a?"|" a[$1]*$NF:"" ##Printing $0(current line) and then checking if $1 of current line is present in array a is yes then print a value * $NF else print NULL.
}
' OFS=""   File2 File1              ##Setting OFS to NULL here and mentioning both the Input_file(s) name here.