想要将第一个文件字段$ 4与第二个文件字段$ 1和第一个文件字段$ 8与第二个文件字段$ 2进行比较。然后仅从第一个文件打印匹配案例。
使用两个单独的命令来达到上述要求,请建议改进并避免两个步骤。
Input.csv
Transaction ID,Request source,User name,SendMobNum,RecMobNum,ServiceClass,Service,Amount,CreditAmount,Bonus,Process fee
ABCD,SSS,EFG,1234,9999,2345,AB,30,1,4,1
ABCD,SSS,EFG,1234,9999,2345,AB,40,2,5,2
ABCD,SSS,EFG,1234,9999,2345,AB,60,3,6,3
ABCD,SSS,EFG,3456,9999,2345,AB,30,1,4,1
ABCD,SSS,EFG,3456,9999,2345,AB,40,2,5,2
ABCD,SSS,EFG,3456,9999,2345,AB,60,3,6,3
ABCD,SSS,EFG,5678,9999,2345,AB,30,1,4,1
ABCD,SSS,EFG,5678,9999,2345,AB,40,2,5,2
ABCD,SSS,EFG,5678,9999,2345,AB,60,3,6,3
master.csv
SendMobNum,Amount
1234,30
5678,15
2345,60
4567
8888
Step_1:使用第二个文件字段$ 1
检查第一个文件字段$ 4awk ' BEGIN {FS = OFS = ","} FNR==NR {a[$1] = $0; next} ($4 in a) {print $0}' master.csv Input.csv >Op_step11.csv
Op_step11.csv
Transaction ID,Request source,User name,SendMobNum,RecMobNum,ServiceClass,Service,Amount,CreditAmount,Bonus,Process fee
ABCD,SSS,EFG,1234,9999,2345,AB,30,1,4,1
ABCD,SSS,EFG,1234,9999,2345,AB,40,2,5,2
ABCD,SSS,EFG,1234,9999,2345,AB,60,3,6,3
ABCD,SSS,EFG,5678,9999,2345,AB,30,1,4,1
ABCD,SSS,EFG,5678,9999,2345,AB,40,2,5,2
ABCD,SSS,EFG,5678,9999,2345,AB,60,3,6,3
Step_2:第一个文件字段$ 8,第二个文件字段$ 2
awk ' BEGIN {FS = OFS = ","} FNR==NR {a[$2] = $0; next} ($8 in a) {print $0}' master.csv Op_step11.csv >Op_step22.csv
Op_step22.csv
Transaction ID,Request source,User name,SendMobNum,RecMobNum,ServiceClass,Service,Amount,CreditAmount,Bonus,Process fee
ABCD,SSS,EFG,1234,9999,2345,AB,30,1,4,1
ABCD,SSS,EFG,1234,9999,2345,AB,60,3,6,3
ABCD,SSS,EFG,5678,9999,2345,AB,30,1,4,1
ABCD,SSS,EFG,5678,9999,2345,AB,60,3,6,3
期望的输出:
Transaction ID,Request source,User name,SendMobNum,RecMobNum,ServiceClass,Service,Amount,CreditAmount,Bonus,Process fee
ABCD,SSS,EFG,1234,9999,2345,AB,30,1,4,1
ABCD,SSS,EFG,1234,9999,2345,AB,60,3,6,3
ABCD,SSS,EFG,5678,9999,2345,AB,30,1,4,1
ABCD,SSS,EFG,5678,9999,2345,AB,60,3,6,3
答案 0 :(得分:1)
awk -F, '
NR == FNR {send[$1]; amt[$2]; next}
FNR == 1 || ($4 in send && $8 in amt)
' master.csv Input.csv
Transaction ID,Request source,User name,SendMobNum,RecMobNum,ServiceClass,Service,Amount,CreditAmount,Bonus,Process fee
ABCD,SSS,EFG,1234,9999,2345,AB,30,1,4,1
ABCD,SSS,EFG,1234,9999,2345,AB,60,3,6,3
ABCD,SSS,EFG,5678,9999,2345,AB,30,1,4,1
ABCD,SSS,EFG,5678,9999,2345,AB,60,3,6,3