如何使用基于模式的底部字符串替换文件顶部的字符串

时间:2015-03-20 02:59:23

标签: string bash replace

我有以下文件:

petro,36262
artur,946034
alex,12345
alex,99999
artur,111111111

我需要的是用基于第一列作为主键的底部字符串替换上部字符串,所以最后它看起来像:

petro,36262
alex,99999
artur,111111111

第一列可以包含空格或数字。

由于

1 个答案:

答案 0 :(得分:1)

这是一种方法:

$ awk -F, '{a[$1]=$2} END{for (name in a)print name","a[name]}' file
alex,99999
artur,111111111
petro,36262

线条以任意顺序出现。如果您有首选订单,则需要一些额外的代码。