简体   繁体   English

AWK,比较两个文件,然后比较匹配的另一个字段并进行算术运算

[英]AWK, Compare of two files then compare the other field for the match one and do arithmetic

File A文件A

Jimmy|03-OCT-18|BST100114261|20000
Dedi|03-OCT-18|BST100904288|10000
Jimmy|03-OCT-18|BST100114262|120000

File B文件B

Anton|9800
Jimmy|90000

Output输出

Jimmy|20000|90000|1800000000|BST100114261
Jimmy|120000|90000|30000|BST100114262

Logic:逻辑:

  1. Find the same Jimmy找到同一个吉米
  2. Compare amount between File A (column 4) and File B (column 2), if File A (column 4) less than File B (column 2) then multiple File A (column 4) and File B (column 2).比较文件 A(第 4 列)和文件 B(第 2 列)之间的数量,如果文件 A(第 4 列)小于文件 B(第 2 列),则多个文件 A(第 4 列)和文件 B(第 2 列)。 If it is less, then deduct File A (column 4) and File B (column 2)如果小于,则扣除文件 A(第 4 栏)和文件 B(第 2 栏)

Note :笔记 :

  • 90000 > 20000 then 20000 * 90000 = 1800000000 90000 > 20000 然后 20000 * 90000 = 1800000000

  • 90000 < 120000 then 120000 - 90000 = 30000 90000 < 120000 然后 120000 - 90000 = 30000

    1. Display output as above显示输出如上

Could you please try following once. 您能否再尝试一次。

awk '
BEGIN{
  FS=OFS="|"
}
FNR==NR{
  a[$1]=$2
  next
}
($1 in a){
  if($4<a[$1]){
     val=$4*a[$1]
     if(val<a[$1]){
       val_new=$4-a[$1]
     }
  }
  else{
     val=$4-a[$1]
  }
  print $1,$4,a[$1],val_new?val_new:val,$3
  val_new=val=""
}
' Input_fileb   Input_filea

Output will be as follows.(Only tested with provided samples in case any conditions then do let us know please) 输出如下(仅在提供样品的情况下进行测试,以防万一,请告知我们)

Jimmy|20000|90000|1800000000|BST100114261
Jimmy|120000|90000|30000|BST100114262
$ awk 'BEGIN   {FS=OFS="|"} 
       NR==FNR {a[$1]=$2;next} 
       $1 in a {v=a[$1]; print $1,$4,v,(v>$4?$4*v:$4-v),$3}' b a

Jimmy|20000|90000|1800000000|BST100114261
Jimmy|120000|90000|30000|BST100114262

Explanation 说明

set the delimiters for parsing and printing the fields 设置用于分析和打印字段的定界符

while scanning the first file (file b) where the linenumber and file line number are equal, fill up the lookup array with key value pairs. 在扫描行号和文件行号相等的第一个文件(文件b)时,用键值对填充查找数组。

while scanning the second file (now the third statement in the script), if the key in in the array, print the fields in desired order and with the described computation. 在扫描第二个文件(现在是脚本中的第三条语句)时,如果键在数组中,则按所需顺序和描述的计算方式打印字段。

To not to rewrite a[$1] multiple times assign it to variable v (for value). 要不多次重写a[$1]请将其分配给变量v (用于值)。

Using Perl one-liner 使用Perl单线

> cat filea
Jimmy|03-OCT-18|BST100114261|20000
Dedi|03-OCT-18|BST100904288|10000
Jimmy|03-OCT-18|BST100114262|120000
> cat fileb
Anton|9800
Jimmy|90000
> perl -F"\|" -lane ' BEGIN { %kvp=map{chomp;split(/\|/)} qx(cat fileb)} { chomp;print "$F[0]|$F[3]|$kvp{$F[0]}|",$F[3]<$kvp{$F[0]}?$F[3]*$kvp{$F[0]}:$F[3]-$kvp{$F[0]},"|$F[2]" if $kvp{$F[0]} } ' filea
Jimmy|20000|90000|1800000000|BST100114261
Jimmy|120000|90000|30000|BST100114262
> 

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

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