2014-08-27 53 views
-1

想要將第一個文件字段$ 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 

回答

1

我真的覺得你可以自己做到這一點,但這裏有一個提示:您想Master.csv像對待2組不同的數據,以便從中填充2個不同的陣列, mob2year[$1]=$2amt2gender[$3]=$4。現在,當您閱讀Input.csv時,只需通過mob2year[$4]amt2gender[$8]即可訪問這些文件。嘗試使用該提示自行創建腳本,並在測試後使用腳本更新您的問題,並在需要幫助時留下評論。

也許這將幫助你理解關聯數組:

$ cat file1 
fruit apple 
color red 
size large 
$ 
$ cat file2 
size fruit garbage color 
$ 
$ awk 'NR==FNR{ a[$1]=$2; next} {print $1, a[$1]}' file1 file2 
size large 
$ awk 'NR==FNR{ a[$1]=$2; next} {print $2, a[$2]}' file1 file2 
fruit apple 
$ awk 'NR==FNR{ a[$1]=$2; next} {print $3, a[$3]}' file1 file2 
garbage 
$ awk 'NR==FNR{ a[$1]=$2; next} {print $4, a[$4]}' file1 file2 
color red 
$ awk 'NR==FNR{ a[$1]=$2; next} {print $5, a[$5]}' file1 file2 

$ 

播放上述內容,增加一些版畫等

+0

埃德莫頓,感謝輸入,對不起,我沒有得到如何打印字段來自Master.csv。 – VNA 2014-08-27 18:45:00

+0

Ed Morton,想將master.csv作爲兩組參考表,一組是前兩個字段,另一組是第三和第四個字段 – VNA 2014-08-27 19:01:19

+0

例如, 如果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