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

前端 未结 6 501
礼貌的吻别
礼貌的吻别 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:15

    Make one call to sed that writes the sed script, and another to use it? If your lists are in files listA and listB, then:

    paste -d : listA listB | sed 's/\([^:]*\):\([^:]*\)/s%\1%\2%/' > sed.script
    sed -f sed.script files.to.be.mapped.*
    

    I'm making some sweeping assumptions about 'words' not containing either colon or percent symbols, but you can adapt around that. Some versions of sed have upper bounds on the number of commands that can be specified; if that's a problem because your word lists are big enough, then you may have to split the generated sed script into separate files which are applied - or change to use something without the limit (Perl, for example).

    Another item to be aware of is sequence of changes. If you want to swap two words, you need to craft your word lists carefully. In general, if you map (1) wordA to wordB and (2) wordB to wordC, it matters whether the sed script does mapping (1) before or after mapping (2).

    The script shown is not careful about word boundaries; you can make it careful about them in various ways, depending on the version of sed you are using and your criteria for what constitutes a word.

提交回复
热议问题