using sed to find and replace in bash for loop

前端 未结 4 1980
一生所求
一生所求 2021-01-06 15:21

I have a large number of words in a text file to replace.

This script is working up until the sed command where I get:

sed: 1: \"*.js\": inval

4条回答
  •  太阳男子
    2021-01-06 15:35

    Another approach, if you don't feel very confident with sed and think you are going to forget in a week what the meaning of that voodoo symbols is, could be using IFS in a more efficient way:

    IFS=":"
    cat myFile.txt | while read PATTERN REPLACEMENT  # You feed the while loop with stdout lines and read fields separated by ":"
    do
       sed -i "s/${PATTERN}/${REPLACEMENT}/g"
    done
    

    The only pitfall I can see (it may be more) is that if whether PATTERN or REPLACEMENT contain a slash (/) they are going to destroy your sed expression. You can change the sed separator with a non-printable character and you should be safe. Anyway, if you know whats on your myFile.txt you can just use any.

提交回复
热议问题