What is an efficient way to replace list of strings with another list in Unix file?

前端 未结 6 478
礼貌的吻别
礼貌的吻别 2020-12-10 03:38

Suppose I have two lists of strings (list A and list B) with the exact same number of entries, N, in each list, and I want to replace all occurrences of the the nth element

6条回答
  •  温柔的废话
    2020-12-10 04:17

    I needed to do something similar, and I wound up generating sed commands based on a map file:

    $ cat file.map
    abc => 123
    def => 456
    ghi => 789
    
    $ cat stuff.txt
    abc jdy kdt
    kdb def gbk
    qng pbf ghi
    non non non
    try one abc
    
    $ sed `cat file.map | awk '{print "-e s/"$1"/"$3"/"}'`<<<"`cat stuff.txt`"
    123 jdy kdt
    kdb 456 gbk
    qng pbf 789
    non non non
    try one 123
    

    Make sure your shell supports as many parameters to sed as you have in your map.

提交回复
热议问题