如何在Tcl脚本中使用grep命令

如何在Tcl脚本中使用grep命令,tcl,Tcl,如何在tcl脚本中运行简单的grep命令并获得输出 grep B file1 > temp # bash grep command need to execute inside tcl commad, 文件1如下所示: 1 2 3 6 180.00 B 1 2 3 6 F 2 3 6 23 50.00 B 2 3 6 23 F 这些不起作用 exec grep B file.txt > temp child process exited abnormally exec

如何在tcl脚本中运行简单的grep命令并获得输出

grep B file1 > temp  # bash grep command need to execute inside tcl commad,
文件1如下所示:

1 2 3 6 180.00 B
1 2 3 6 F 
2 3 6 23 50.00 B 
2 3 6 23 F
这些不起作用

exec grep B file.txt > temp
child process exited abnormally


exec "grep B pes_test.com > temp1"
couldn't execute "grep -e B ./pes_test.com > temp1": no such file or directory

exec /bin/sh -c {grep -e B ; true} < pes_test.com > tmp1
works but do not gives output, 
exec grep B file.txt>temp
子进程异常退出
执行官“grep B pes_test.com>temp1”
无法执行“grep-eb./pes_test.com>temp1”:没有这样的文件或目录
exec/bin/sh-c{grep-eb;true}tmp1
工作但不输出,

exec
在进程返回非零时抛出错误。看到和


Ref:

如果文件中存在要查找的模式,则不会得到“子进程异常退出”。看来你只是在处理错误的文件;file.txt而不是file1。为什么要将外部程序拖到其中?打开一个文件,读一行,并在TCL中与每一个匹配一个正则表达式是微不足道的。好的,谢谢,ShawnYou也可以考虑使用TCLIB中的<代码> FieluTyl < /Cult>包。它有一个
grep
命令来完成繁重的工作。非常令人恼火的是
CHILDSTATUS
错误代码将进程ID放在退出代码之前。
try {
    set result [exec grep $pattern $file]
} on error {e} {
    # typically, pattern not found
    set result ""
}