2013-03-22 57 views
0

file1.txt低于AWK:在不同势文件搜索字符串,然后追加结果输出

30012516|Geralyn|test|1010029|9985|0029|50|00|OneTime|1227065|2013-03-04|||||||Code4 
30013017|tamara|test|3440029|1114|029|41|00|OneTime|1239244|2013-03-04|||||||Code3 
30015518|daniel|test|3140029L|6440|0029|99|00|OneTime|1239306|2013-03-03|||||||Code2 
30050011|first|test|1240030|1745|030|96|00|OneTime|1284010|2013-02-22|||||||Code1 
10010905|madhu|com|5230029|614|029|29|10|OneTime|1293016|2013-03-04|||||||Code5 

这是另一种file2.txt包含上述file1.txt

Code1=Results of code1 
Code3=Results of code3 
Code2=Results of code2 
Code5=Results of code5 
Code6=Results of code6 
Code4=Results of code4 
Code7=Results of code7 
Code8=Results of code8 
Code9=Results of code9 
Code10=Results of code10 

我的最后一个字符串的值想要使用awk命令首先搜索file2.txtfile1.txt的最后一个字符串,并将结果值附加到下面(这应该是最终输出)

30012516|Geralyn|test|1010029|9985|0029|50|00|OneTime|1227065|2013-03-04|||||||Code4|Results of code4 
30013017|tamara|test|3440029|1114|029|41|00|OneTime|1239244|2013-03-04|||||||Code3|Results of code3 
30015518|daniel|test|3140029L|6440|0029|99|00|OneTime|1239306|2013-03-03|||||||Code2|Results of code2 
30050011|first|test|1240030|1745|030|96|00|OneTime|1284010|2013-02-22|||||||Code1|Results of code1 
10010905|madhu|com|5230029|614|029|29|10|OneTime|1293016|2013-03-04|||||||Code5|Results of code5 

我想下面的命令,但它不仅赋予了价值,但不能在file1.txt

awk -F= 'NR==FNR{A[$1]=$2; next} $NF in A{print A[$NF]}' file2.txt FS=\| file1.txt > output.out 

回答

1

结果追加尝试这一行:

awk -F= 'NR==FNR{a[$1]=$2;next} {for(x in a) if($0~"\\|"x"$"){print $0"|"a[x];break}}' file2 file1 

还是有一点固定在你的声明:

awk -F= 'NR==FNR{A[$1]=$2; next} $NF in A{print $0,A[$NF]}' file2.txt FS=\| OFS="|" file1.txt 
+0

Thanks @Kent its工作! – Rakesh 2013-03-22 09:49:49

相关问题