想要將第一個文件字段$ 4與第二個文件字段$ 1進行比較,並且將第一個文件字段$ 8與第二個文件字段$ 3進行比較。 然後從第一個文件和IF字段$ 1匹配打印匹配案例,然後從第二個文件打印對應字段$ 2, 字段$ 3匹配,然後從第二個文件打印相應字段$ 4。比較兩個文件和兩個字段 - 續:
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,Year,Amount,Gender
1234,2000,30,Male
5678,2001,15,Female
2345,2002,60,Female
4567,2003
8888,2004
所需的輸出:
Transaction ID,Request source,User name,SendMobNum,RecMobNum,ServiceClass,Service,Amount,CreditAmount,Bonus,Process fee,SendMobNum,Year,Amount,Gender
ABCD,SSS,EFG,1234,9999,2345,AB,30,1,4,1,1234,2000,30,Male
ABCD,SSS,EFG,1234,9999,2345,AB,60,3,6,3,1234,2000,60,Female
ABCD,SSS,EFG,5678,9999,2345,AB,30,1,4,1,5678,2001,30,Male
ABCD,SSS,EFG,5678,9999,2345,AB,60,3,6,3,5678,2001,60,Female
是否嘗試以下命令和部分:
awk -F, '
NR == FNR {send[$1]; amt[$3]; next}
FNR == 1 || ($4 in send && $8 in amt) { print $0","send[$1] ","send[$2]","amt[$3]","amt[$4]}
' master.csv Input*.csv
點
任何建議...
編輯:想治療mater.csv作爲兩個不同的數據集,
集#1
SendMobNum,Year(i.e Desc of SendMobNum)
1234,2000
5678,2001
2345,2002
4567,2003
8888,2004
集#2
Amount,Gender (i.e Desc of Amount)
30,Male
15,Female
60,Female
示例#1:如果Input.Field $ 4 == 1234和Input.Field $ 8 == 30
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
運算#1:
Transaction ID,Request source,User name,SendMobNum,RecMobNum,ServiceClass,Service,Amount,CreditAmount,Bonus,Process fee,SendMobNum,Year,Amount,Gender
ABCD,SSS,EFG,1234,9999,2345,AB,30,1,4,1,1234,2000,30,Male
實施例#2:如果Input.Field $ 4 == 1234和Input.Field $ 8 == 15
Transaction ID,Request source,User name,SendMobNum,RecMobNum,ServiceClass,Service,Amount,CreditAmount,Bonus,Process fee
ABCD,SSS,EFG,1234,9999,2345,AB,15,1,4,1
運算#2:
Transaction ID,Request source,User name,SendMobNum,RecMobNum,ServiceClass,Service,Amount,CreditAmount,Bonus,Process fee,SendMobNum,Year,Amount,Gender
ABCD,SSS,EFG,1234,9999,2345,AB,15,1,4,1,1234,2000,15,Female
示例#3:如果Input.Field $ 4 == 1234和Input.Field $ 8 == 60,則打印第二個文件1234,2000,60,女
Transaction ID,Request source,User name,SendMobNum,RecMobNum,ServiceClass,Service,Amount,CreditAmount,Bonus,Process fee
ABCD,SSS,EFG,1234,9999,2345,AB,60,1,4,1
運算#3:
Transaction ID,Request source,User name,SendMobNum,RecMobNum,ServiceClass,Service,Amount,CreditAmount,Bonus,Process fee,SendMobNum,Year,Amount,Gender
ABCD,SSS,EFG,1234,9999,2345,AB,60,1,4,1,1234,2000,60,Female
更新:2014年8月28日
哇,非常感謝埃德莫頓非常好的提示,全國聯保! 我嘗試過試用和錯誤的基礎,得到下面的輸出。 我在使用數組時遇到困惑,無法理解數組的概念,如何調試或檢查命令,是否正在訪問第一個文件第一行然後檢查第二個文件的整個文件等等......
嘗試#1 :(沒有$ 1 & $ 3從主。CSV)
awk '
BEGIN{ FS=OFS="," }
NR == FNR { mob2year[$1]=$2;amt2gender[$3]=$4; next}
FNR == 1 || ($4 in mob2year && $8 in amt2gender) { print $0,mob2year[$4],amt2gender[$8] }
' Master.txt Input*.txt
輸出:
Transaction ID,Request source,User name,SendMobNum,RecMobNum,ServiceClass,Service,Amount,CreditAmount,Bonus,Process fee,Year,Gender
ABCD,SSS,EFG,1234,9999,2345,AB,30,1,4,1,2000,Male
ABCD,SSS,EFG,1234,9999,2345,AB,60,3,6,3,2000,Female
ABCD,SSS,EFG,5678,9999,2345,AB,30,1,4,1,2001,Male
ABCD,SSS,EFG,5678,9999,2345,AB,60,3,6,3,2001,Female
嘗試#2:(無$ 1 & $ 3從master.csv但填充從Input.csv的信息)想知道如何打印$ 1 $ 3從master.csv
awk '
BEGIN{ FS=OFS="," }
NR == FNR { mob2year[$1]=$2;amt2gender[$3]=$4; next}
FNR == 1 || ($4 in mob2year && $8 in amt2gender) { print $0,$4,mob2year[$4],$8,amt2gender[$8] }
' Master.txt Input*.txt
輸出:
Transaction ID,Request source,User name,SendMobNum,RecMobNum,ServiceClass,Service,Amount,CreditAmount,Bonus,Process fee,SendMobNum,Year,Amount,Gender
ABCD,SSS,EFG,1234,9999,2345,AB,30,1,4,1,1234,2000,30,Male
ABCD,SSS,EFG,1234,9999,2345,AB,60,3,6,3,1234,2000,60,Female
ABCD,SSS,EFG,5678,9999,2345,AB,30,1,4,1,5678,2001,30,Male
ABCD,SSS,EFG,5678,9999,2345,AB,60,3,6,3,5678,2001,60,Female
評論:
awk '
BEGIN{ FS=OFS="," } 'Assign Input/Output separator as ","
NR == FNR { mob2year[$1]=$2;amt2gender[$3]=$4; next} 'Create array mob2year and store $1 & $2 unique values from Master.txt then Create array amt2gender and store $3 & $4 unique values from Master.txt
'Read all the lines from Master.txt store into mob2year and amt2gender
FNR == 1 || ($4 in mob2year && $8 in amt2gender) 'If NR==1 OR ($4 from Input.txt in mob2year array AND $8 from amt2gender) then
{ print $0,$4,mob2year[$4],$8,amt2gender[$8] } 'print entire line from Input.txt ($0) , $4 from Input.txt, $8 from Input.txt
'Not able to understand mob2year[$4] and amt2gender[$8] logic values
' Master.txt Input*.txt
埃德莫頓,感謝輸入,對不起,我沒有得到如何打印字段來自Master.csv。 – VNA 2014-08-27 18:45:00
Ed Morton,想將master.csv作爲兩組參考表,一組是前兩個字段,另一組是第三和第四個字段 – VNA 2014-08-27 19:01:19
例如, 如果Input.Field $ 4 = = 1234和Input.Field $ 8 == 30然後打印第二個文件1234,2000,30,男 如果輸入。 Field $ 4 == 1234和Input.Field $ 8 == 15然後打印第二個文件1234,2000,15,女性 如果Input.Field $ 4 == 1234和Input.Field $ 8 == 60,則打印第二個文件1234,2000,60 ,女 – VNA 2014-08-27 19:09:21