英文:
Sed command not work in Tcl script but OK in shell
问题
任务是搜索特定关键字,然后转到下一行,将括号内的最后两个数字/字符串替换为其他数字/字符串。
文本文件内容如下:
> test text Some_Text_Pattern\
> { 7.315e-12 3.261963e-11 1.454601e-10 6.486475e-10 2.8925e-09 }
some other test text Some_Text_Pattern\
> { 7.315e-12 3.261963e-11 1.454601e-10 6.486475e-10 2.8925e-09 }
我使用的命令如下:
foreach f $files {
# 以下是我想使用但失败的复杂版本
exec /bin/sh -c "sed '/Some_Text_Pattern\\/{n; s/[0-9.]\+[eE\][+-][0-9]\+\s*[0-9.]\+[eE][+-][0-9]\+\s*}/1.000e-9 3.000e-9 \} /}' ${dir}/{f}.txt"
}
出现各种错误。最新的一个错误消息如下:
sed: -e expression #1, char 41: unknown command: `['
while executing "exec /bin/sh -c "sed '/Some_Text_Pattern\\/{n; s/[0-9.]\+[eE\][+-][0-9]\+\s*[0.9.]\+[eE][+-][0-9]\+\s*}/1.000e-9 3.000e-..."""
(for each body line 3)
英文:
The task is to search certain keyword then goto next line, replace last two numbers/strings within braces, with other numbers/strings.
Text file content is like:
"
> test text Some_Text_Pattern
> { 7.315e-12 3.261963e-11 1.454601e-10 6.486475e-10 2.8925e-09 }
some other test text Some_Text_Pattern
> { 7.315e-12 3.261963e-11 1.454601e-10 6.486475e-10 2.8925e-09 }
The command I'm using are like:
foreach f $files {
# Below is a complex version which I want to use but failed
exec /bin/sh -c "sed '/Some_Text_Pattern\\/{n; s/[0-9.]\+[eE\][+-][0-9]\+\s*[0-9.]\+[eE][+-][0-9]\+\s*\}/1.000e-9 3.000e-9 \} /}' ${dir}/{f}.txt"
}
Errors are all kinds. Latest one says:
> sed: -e expression #1, char 41: unknown command: `['
> while executing "exec /bin/sh -c "sed '/constraint_explicit_points_slew\/{n;
> s/[0-9.]+[eE][+-][0-9]+\s*[0-9.]+[eE][+-][0-9]+\s*}/1.000e-9 3.000e-..."
> ("foreach" body line 3)
答案1
得分: 1
我得到的一个解决方案:
exec sed -i {/某些文本模式\\/{n; s/[0-9.]\+[eE\][+-][0-9]\+\s*[0-9.]\+[eE][+-][0-9]\+\s*\}/1.000e-9 3.000e-9\}/}} ${file}
在Tcl中使用shell命令似乎更加复杂。
英文:
One solution I got:
exec sed -i {/Some_Text_Pattern\\/{n; s/[0-9.]\+[eE\][+-][0-9]\+\s*[0-9.]\+[eE][+-][0-9]\+\s*\}/1.000e-9 3.000e-9\}/}} ${file}
Seems using shell command in Tcl is more complicated.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论