我有一个字符串,我想在脚本中替换它以放置文件的实际路径,因此:我的问题是该字符串里面有特殊字符
Dir=pwd
old=simulationfoam = OpenFOAMReader(registrationName='simulation.foam', FileName='path/simulation.foam')
new=simulationfoam = OpenFOAMReader(registrationName='simulation.foam', FileName='${Dir}/simulation.foam')
我首先要引用所有内容:
old='simulationfoam = OpenFOAMReader(registrationName='simulation.foam', FileName='path/simulation.foam')'
new='simulationfoam = OpenFOAMReader(registrationName='simulation.foam', FileName='${Dir}/simulation.foam')'
但在网上查找后问题仍然存在,我发现了这一点:https://stackoverflow.com/questions/15783701/which-characters-need-to-be-escaped-when-using-bash 其中提到我应该用“反斜杠”来转义“,所以我用“反斜杠”替换了字符串的内部“
答案1
我通过在 seq 行中使用 | 代替 / 来解决:
sed -i "19s|simulationfoam = OpenFOAMReader(registrationName='simulation.foam', FileName='PATH/simulation.foam')|simulationfoam = OpenFOAMReader(registrationName='simulation.foam', FileName='$(pwd)/simulation.foam')|" ./code.py