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

前端 未结 4 1723
没有蜡笔的小新
没有蜡笔的小新 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:24

    Here are the basic steps I would do

    1. Copy the changesDictionary.txt file
    2. In it replace "a"="b" to the equivalent sed line: e.g. (use $1 for the file name)

      sed -e 's/a/b/g' $1

      (you could write a script to do this or just do it by hand, if you just need to do this once and it's not too big).

    3. If the files are all in one directory, then you can do something like:

      ls *.txt | xargs scriptFromStep2.sh

    4. If they are in subdirs, use a find to call that script on all of the files, something like

      find . -name '*.txt' -exec scriptFromStep2.sh {} \;

    These aren't exact, do some experiments to make sure you get it right -- it's just the approach I would use.

    (but, if you can, just use perl, it would be a lot simpler)

提交回复
热议问题