英文:
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 ofEssays
into new folderEssaysCopy
and then perform the replacement of string with contents ofspecialPara.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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论