0

我有以下文件1

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下面搜索$18(即code{x}),如果找到匹配则输出file2的对应值

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

然后最终输出将是

Results of code4
Results of code3
Results of code2
Results of code1
Results of code5

我试过下面的 grep 命令来获取对应键的值

"(grep '"$18"=' file2 | cut -d'=' -f2)" | getline result 

它工作正常,但对于几个键它没有返回正确的值。有没有其他方法可以获得正确的结果

4

1 回答 1

2

您可以尝试这种 awk 方法:

awk -F= 'NR==FNR{A[$1]=$2; next} $NF in A{print A[$NF]}' file2 FS=\| file1
于 2013-03-06T13:14:19.747 回答