“Sed命令在Tcl脚本中不起作用,但在shell中正常。”

huangapple go评论55阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月26日 17:16:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76339356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定