我想在TCL脚本中打印文件中的特定列号字段.
我尝试使用exec awk '{print $4}' foofoo文件名,但它没有工作,因为它给出了错误
can't read "4": no such variable
如何在tcl脚本中执行awk?
谢谢,
gle*_*man 14
问题是单引号在Tcl中没有特殊含义,它们只是字符串中的普通字符.因此,$4它不会从Tcl中隐藏,它会尝试扩展变量.
相当于shell单引号的Tcl是大括号.这就是你需要的:
exec awk {{print $4}} foo
Run Code Online (Sandbox Code Playgroud)
双括号看起来很有趣,但外对用于Tcl,内对用于awk.
顺便说一下,awk程序的Tcl翻译是:
set fid [open foo r]
while {[gets $fid line] != -1} {
set fields [regexp -all -inline {\S+} $line]
puts [lindex $fields 3]
}
close $fid
Run Code Online (Sandbox Code Playgroud)