How to recursively replace a string in all files of a folder with the contents of specific file outside the folder using bash script?

huangapple go评论68阅读模式
英文:

How to recursively replace a string in all files of a folder with the contents of specific file outside the folder using bash script?

问题

以下是您提供的内容的中文翻译:

问题:

  • 我有一个文件夹(Essays),其中包含许多嵌套的子目录和“.txt”文件。
  • 仅有某些文件在其内容中包含字符串“[[RepeatedPara]]”。
  • 在文件夹外部,我有另一个文本文件specialPara.txt,其中包含需要替换所有文件中的“[[RepeatedPara]]”字符串的段落。
  • 我创建了一个名为“addPara.sh”的脚本文件,该文件应首先将Essays的内容复制到新文件夹EssaysCopy中,然后执行将字符串替换为specialPara.txt内容的操作。

文件夹结构

.
└── WorkingDir/
    ├── addPara.sh
    ├── specialPara.txt
    ├── EssaysCopy
    └── Essays/
        ├── file-01.txt
        ├── file-02.txt
        ├── file-03.txt
        ├── subDir-01/
        │   ├── file-01-01.txt
        │   ├── file-01-02.txt
        │   └── subSubDir-01-01/
        │       └── file-01-01.txt
        └── subDir-02/
            ├── file-02-01.txt
            └── file-02-02.txt

到目前为止我拥有的脚本:

在查看了stackoverflow上的一个答案后,我创建了以下脚本。

#!/bin/sh

cp -r Essays EssaysCopy
cd EssaysCopy
for FILE in $(find *.txt -type f);
do
sed -i -e '/RepeatedPara/{r ../specialPara.txt' -e 'd' -e '}' $FILE
done

结果:

上述脚本确实会替换文件夹EssaysCopy中所有文件中包含字符串RepeatedPara的行,但不会处理嵌套子目录中的文件。

我用于创建测试嵌套目录和文件的脚本:

#!/bin/sh

mkdir Essays
cd Essays
for i in {1..3}
do
mkdir subDir-$i
cd subDir-$i
for j in {1..2}
do
    mkdir subDir-$j
	cd subDir-$j
	for k in {1..2}
	do
		echo "Some Content at the begining" > file-$i-$j-$k.txt
		echo "[[RepeatedPara]]" >> file-$i-$j-$k.txt
		echo "Some Content at the End" >> file-$i-$j-$k.txt
	done
	cd ..
done
for l in {1..2}
do
	echo "Some Content at the begining" > file-$i-$l.txt
	echo "[[RepeatedPara]]" >> file-$i-$l.txt
	echo "Some Content at the End" >> file-$i-$l.txt
done
cd ..
done
for m in {1..2}
do
    echo "Some Content at the begining" > file-$m.txt
    echo "[[RepeatedPara]]" >> file-$m.txt
    echo "Some Content at the End" >> file-$m.txt
done
cd ..

请注意,我只提供了翻译,没有包括其他附加内容。

英文:

Problem:

  • I have a folder (Essays) containing many nested sub-directories and ".txt" files.
  • Only some files within this folder contain the string '[[RepeatedPara]]' somewhere among the content.
  • Outside the folder I have another text file specialPara.txt which contains the paragraph that needs to replace [[RepeatedPara]] string in all the files.
  • I have created a script file "addPara.sh" which should first copy the contents of Essays into new folder EssaysCopy and then perform the replacement of string with contents of specialPara.txt.

Folder Structure

.
└── WorkingDir/
    ├── addPara.sh
    ├── specialPara.txt
    ├── EssaysCopy
    └── Essays/
        ├── file-01.txt
        ├── file-02.txt
        ├── file-03.txt
        ├── subDir-01/
        │   ├── file-01-01.txt
        │   ├── file-01-02.txt
        │   └── subSubDir-01-01/
        │       └── file-01-01.txt
        └── subDir-02/
            ├── file-02-01.txt
            └── file-02-02.txt

Script I have so far:

After looking into an answer at stackoverflow I created the following script.

#!/bin/sh

cp -r Essays EssaysCopy
cd EssaysCopy
for FILE in $(find *.txt -type f);
do
sed -i -e '/RepeatedPara/{r ../specialPara.txt' -e 'd' -e '}' $FILE
done

Result:

The above script does replace the line containing the string RepeatedPara in all files of folder EssaysCopy but doesn't process the files within the nested sub-directories.

Script I use for creating nested Directories and files for testing:

#!/bin/sh

mkdir Essays
cd Essays
for i in {1..3}
do
mkdir subDir-$i
cd subDir-$i
for j in {1..2}
do
    mkdir subDir-$j
	cd subDir-$j
	for k in {1..2}
	do
		echo "Some Content at the begining" > file-$i-$j-$k.txt
		echo "[[RepeatedPara]]" >> file-$i-$j-$k.txt
		echo "Some Content at the End" >> file-$i-$j-$k.txt
	done
	cd ..
done
for l in {1..2}
do
	echo "Some Content at the begining" > file-$i-$l.txt
	echo "[[RepeatedPara]]" >> file-$i-$l.txt
	echo "Some Content at the End" >> file-$i-$l.txt
done
cd ..
done
for m in {1..2}
do
    echo "Some Content at the begining" > file-$m.txt
    echo "[[RepeatedPara]]" >> file-$m.txt
    echo "Some Content at the End" >> file-$m.txt
done
cd ..

答案1

得分: 2

If it's a bash script, use a bash shebang.

Then should use find's -exec and quote the glob expression:

#!/bin/bash

cp -r Essays EssaysCopy
cd EssaysCopy
find . -type f -name "*.txt" -exec \
  sed -i -e '/RepeatedPara/{r ../specialPara.txt' -e 'd' -e '}' {} \;
英文:

If it's a bash script, use a bash shebang.

Then should use find's -exec and quote the glob expression:

#!/bin/bash

cp -r Essays EssaysCopy
cd EssaysCopy
find . -type f -name "*.txt" -exec \
  sed -i -e '/RepeatedPara/{r ../specialPara.txt' -e 'd' -e '}' {} \;

assuming, as you mentioned that your sed command works as expected.

huangapple
  • 本文由 发表于 2023年5月15日 15:28:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定