Shell script - search and replace text in multiple files using a list of strings

前端 未结 4 1781
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 11:47

I have a file \"changesDictionary.txt\" containing (a variable number of) pairs of key-value strings.

e.g.

\"textToSearchFor\" = \"theReplacementText\"

4条回答
  •  悲哀的现实
    2021-01-02 12:22

    #!/bin/bash
    f="changesDictionary.tx"
    find /path -type f -name "*.txt" | while read FILE 
    do
        awk 'BEGIN{ FS="=" }
        FNR==NR{ s[$1]=$2;  next }
        {
           for(i in s){      
            if( $0 ~ i ){ gsub(i,s[i]) }
           }
           print $0
        }' $f $FILE  > temp
        mv temp $FILE
    done
    

提交回复
热议问题