awk匹配来自两个文件

时间:2017-11-09 11:20:22

标签: awk

希望将基于字段$2 (Country)和字段$4 (Type)的Input.csv文件与Ref.csv匹配 如果匹配,则需要从Input.csv打印整行,并从Ref.csv打印相应的Amount字段 如果不匹配,则从Input.csv和“NotFound”

打印整行
For example: Country= "Angola" and Type="Voice"  then need to print the value from $3 (Amount=10) Ref.csv
         Country= "Angola" and Type="Data"   then need to print the value from $4 (Amount=20) Ref.csv   
         Country= "Angola" and Type="SMS"    then need to print the value from $5 (Amount=30) Ref.csv   

         Country= "Yemen" is not available in the Ref.csv , so need to print as "NotFound" 

Input.csv

Month,Country,Desc,Type
Nov'2017,Angola,abc,Voice
Nov'2017,Angola,xxx,Voice
Nov'2017,Angola,abc,Data
Nov'2017,Angola,abc,SMS
Nov'2017,Brazil,def,Data
Nov'2017,Brazil,def,Voice
Nov'2017,Yemen,yyy,Data
Nov'2017,Zambia,ghi,SMS
Nov'2017,Zambia,xxx,SMS
Nov'2017,Zimbabwe,yyy,Voice

Ref.csv

Country,Desc,Voice,Data,SMS
Albania,abc,5,10,15
Angola,def,10,20,30
Bahrain,ghi,2,4,6
Brazil,jkl,45,30,15
Zambia,mno,5,7,9

预期的Output.csv

Month,Country,Desc,Type,Amount
Nov'2017,Angola,abc,Voice,10
Nov'2017,Angola,xxx,Voice,10
Nov'2017,Angola,abc,Data,20
Nov'2017,Angola,abc,SMS,30
Nov'2017,Brazil,def,Data,30
Nov'2017,Brazil,def,Voice,45
Nov'2017,Yemen,yyy,Data,NotFound
Nov'2017,Zambia,ghi,SMS,9
Nov'2017,Zambia,xxx,SMS,9
Nov'2017,Zimbabwe,yyy,Voice,NotFound

寻找你的建议...... !!!

编辑命令#1

awk 'BEGIN  { FS=OFS="," }
      NR==1{print $0,"Amount"; next} {
      map[$1,"Voice"]=$3
      map[$1,"Data"]=$4
      map[$1,"SMS"]=$5
(($2","$4) in map ) { print $0,map[f2,f4] }  else { print $0,"NotFound" }
}' Ref.csv Input.csv

2 个答案:

答案 0 :(得分:1)

awk -F, -v OFS=, '
    NR == FNR {
        for (i=3; i<=NF; i++)
            if (FNR == 1)
                head[i] = $i
            else
                ref[$1,head[i]] = $i
        next
    }
    FNR == 1 {print $0, "Amount"; next}
    {
        amount = (($2,$4) in ref) ? ref[$2,$4] : "NotFound"
        print $0, amount
    }
' Ref.csv Input.csv

答案 1 :(得分:0)

在输入文件所在的同一目录中运行此脚本。

#!/bin/sh
cat Input.csv | while read line
do
  country=`echo $line | awk -F ',' '{print $2}'`
  type=`echo $line | awk -F ',' '{print $4}'`
  ref=`grep -i $country Ref.csv`

  if [ -z "$ref" ];
  then
    echo $line,NotFound;
  else
    case $type in
      "Voice")
        data=`echo $ref | awk -F ',' '{print $3}'`
      ;;
    esac
    case $type in
      "Data")
        data=`echo $ref | awk -F ',' '{print $4}'`
      ;;
    esac
    case $type in
      "SMS")
        data=`echo $ref | awk -F ',' '{print $5}'`
      ;;
    esac

    echo $line,$data; 
  fi;

done;

我测试了它,它输出了你想要的。