【问题标题】:how do I replace the string from the top of the file with the bottom string based on pattern如何根据模式用底部字符串替换文件顶部的字符串
【发布时间】:2015-05-23 09:32:59
【问题描述】:

我有以下文件:

石油,36262 阿图尔,946034 亚历克斯,12345 亚历克斯,99999 阿图尔,111111111

我需要的是根据第一列作为主键将上面的字符串替换为下面的字符串,所以最后它看起来像:

石油,36262 亚历克斯,99999 阿图尔,111111111

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

谢谢

【问题讨论】:

    标签: string bash replace


    【解决方案1】:

    这是一种方法:

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

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

    【讨论】:

      猜你喜欢
      • 2019-02-18
      • 2011-07-28
      • 1970-01-01
      • 2013-10-24
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 2022-12-28
      • 1970-01-01
      相关资源
      最近更新 更多