Optimize shell script for multiple sed replacements

前端 未结 7 1906
清歌不尽
清歌不尽 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 08:54

    You can cut down unnecessary awk invocations and use BASH to break name-value pairs:

    while IFS='|' read -r old new; do
       # echo "$old :: $new"
       sed -i "s~$old~$new~g" file
    done < replacement_list
    

    IFS='|' will give enable read to populate name-value in 2 different shell variables old and new.

    This is assuming ~ is not present in your name-value pairs. If that is not the case then feel free to use an alternate sed delimiter.

提交回复
热议问题