Optimize shell script for multiple sed replacements

前端 未结 7 1896
清歌不尽
清歌不尽 2020-12-19 08:50

I have a file containing a list of replacement pairs (about 100 of them) which are used by sed to replace strings in files.

The pairs go like:



        
7条回答
  •  再見小時候
    2020-12-19 09:14

    You might want to do the whole thing in awk:

    awk -F\| 'NR==FNR{old[++n]=$1;new[n]=$2;next}{for(i=1;i<=n;++i)gsub(old[i],new[i])}1' replacement_list file
    

    Build up a list of old and new words from the first file. The next ensures that the rest of the script isn't run on the first file. For the second file, loop through the list of replacements and perform them each one by one. The 1 at the end means that the line is printed.

提交回复
热议问题