我有一个要替换的字符串。格式为:
VAL1 = "D_AC" ,
VAL2 = "DRC" ,
VALX2 = 3.33330000e+04, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
请注意,该字符串同时包含双引号和换行符。
我想用另一个字符串替换它:
VAL1 = "D_AC" ,
VAL2 = "DRC" ,
VALX2 = 2.22110000e+04, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
我尝试过:
export OLD_STRING1=" VAL1 = \"D_AC\" ,"
export OLD_STRING2=" VAL2 = \"DRC\" ,"
export OLD_STRING3=" VALX2 = 3.33330000e+04, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, "
export NEW_STRING1=" VAL1 = \"D_AC\" ,"
export NEW_STRING2=" VAL2 = \"DRC\" ,"
export NEW_STRING3=" VALX2 = 2.22110000e+04, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, "
然后这样称呼grep
和sed
:
find . -type f -name '*filename*' -exec sed -i '' s/$OLD_STRING1$OLD_STRING2$OLD_STRING3/$NEW_STRING1$NEW_STRING2$NEW_STRING3/ {} +
sed
生成错误:
sed: 1: "s/BX2 = "D_AC ...": unterminated substitute pattern
我该如何解决?
答案 0 :(得分:1)
由于sed无法理解文字字符串(请参阅Is it possible to escape regex metacharacters reliably with sed),因此尝试对sed进行健壮和可移植的工作将是一场噩梦。
在要更改file
的位置输入此内容,以使old
中包含的文本成为new
中包含的文本:
$ cat old
VAL1 = "D_AC" ,
VAL2 = "DRC" ,
VALX2 = 3.33330000e+04, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
$
$ cat new
VAL1 = "D_AC" ,
VAL2 = "DRC" ,
VALX2 = 2.22110000e+04, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
$
$ cat file
foo
VAL1 = "D_AC" ,
VAL2 = "DRC" ,
VALX2 = 3.33330000e+04, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
bar
和此脚本:
$ cat tst.awk
FILENAME==ARGV[1] { old=$0; next }
FILENAME==ARGV[2] { new=$0; next }
s=index($0,old) {
$0 = substr($0,1,s-1) new substr($0,s+length(old))
}
{ print }
就这么简单:
$ awk -v RS= -f tst.awk old new file
foo
VAL1 = "D_AC" ,
VAL2 = "DRC" ,
VALX2 = 2.22110000e+04, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
bar