[英]Bash script find pattern and replace by filename
I'm trying to find all files of a specific folder and which contains text "TO_BE_REPLACED" and I would like to replace this string by the filename without the characters Impl. 我正在尝试查找特定文件夹的所有文件,其中包含文本“ TO_BE_REPLACED”,我想用不带字符Impl的文件名替换此字符串。 I mean, having a file called MyServiceImpl with text "TO_BE_REPLACED" I would like to replace it with the text "MyService". 我的意思是,有一个名为MyServiceImpl的文本“ TO_BE_REPLACED”的文件,我想将其替换为文本“ MyService”。 How I can achieve that? 我该如何实现? I tried this way without results: 我尝试过这种方法,但没有结果:
find src/main/java/com/shn/*/server/service/impl/ -type f -print0 | xargs -0 sed -i "" "s/TO_BE_REPLACED/<filename>/g"
You can do it like this: 您可以这样做:
while read -r filename
do
sed -i "s/TO_BE_REPLACED/MyService/g" $filename
done < <(find "src/main/java/com/shn/*/server/service/impl/" -type f)
If you have GNU Parallel >= 20140822: 如果您具有GNU Parallel> = 20140822:
find . -type f | parallel -q sed -i "" -e 's/TO_BE_REPLACED/{=s:.*/::;s/Impl$//=}/g' {}
The trick here is to use a perl expression to go from .../MyServiceImpl
to MyService
and use that in sed
. 这里的技巧是使用perl表达式从.../MyServiceImpl
到MyService
并在sed
使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.