[英]xargs -I replace-str in find … -exec sed
I'm on GNU/Linux system with Bash 4.4.23 and I have a file with some Bash variables: 我在使用Bash 4.4.23的GNU / Linux系统上,并且我有一个包含一些Bash变量的文件:
ex. 恩。
export VARONE="var1"
export VARTWO="var2"
...
I have to replace the ${VARIABLE}
with {{VARIABLE}}
in other files with ".template" extension under "templates" folder. 我必须将其他文件中的${VARIABLE}
替换为{{VARIABLE}}
,扩展名为“ templates”文件夹下的“ .template”。
I try to execute this command: 我尝试执行以下命令:
cat varfile \
| grep export \
| awk -F '=' '{print $1}' \
| awk '{print $2}' \
| xargs -I var -- \
find templates -type f -iname "*.template" \
-exec sed 's/${var}/{{var}}/g' {} \;
but I had no luck :( it seems that sed does not match with this LHS, infact it prints the original file as output. 但是我没有运气:(似乎sed与这个LHS不匹配,实际上它会将原始文件打印为输出。
Someone can explain what is wrong with the command above? 有人可以解释上面的命令有什么问题吗?
The only issue seems like the lack of -i
flag and the unescaped $
for sed. 唯一的问题似乎是缺少-i
标志和sed的未转义$
。
Here's a simpler, tested, initial pipeline with aforementioned fixes: 这是带有上述修复程序的更简单,经过测试的初始管道:
sed -nE '/^export /{s///;s/([^=]+).*/\1/;p;}' varfile |
xargs -I var -- find templates -name '*.template' -exec sed -i '' 's/\${var}/{{var}}/g' {} +
For assurance, I added a g
flag if the variable appears more than once per line. 为了保证,如果变量每行出现多次,我添加了一个g
标志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.