我一直在尝试使用shell脚本和perl替换.txt文件中的某些文本。
oldKey=123
trimmedNewKey=456
#Export shell variables, so that they can be used by perl
export oldKey
export trimmedNewKey
#Search and Replace
perl -pi -e 's/$ENV{oldKey}/$ENV{trimmedNewKey}/g' AppConstants.txt
这会失败,但是另一方面,如果我直接将字符串用作搜索参数,则它会起作用:
perl -pi -e 's/123/$ENV{trimmedNewKey}/g' AppConstants.txt.
我不是一个空壳小子,无法弄清楚为什么无法通过变量对我的“搜索”参数进行求值。救命!
答案 0 :(得分:-1)
尝试将代码更改为
oldKey=123
trimmedNewKey=456
perl -pi -e "s/$oldKey/$trimmedNewKey/g" AppConstants.txt
这应该起作用,因为在用单引号引起来的字符串中没有变量和通配符扩展,就像在用双引号引起来的字符串中那样。
也许可以将一些变量传递给perl,以便在perl程序内部进行扩展,但是当您可以更轻松地解决它时,为什么要使其复杂化呢?