将变量作为参数传递给TCL中的awk文件

时间:2015-05-09 15:01:01

标签: variables awk parameters tcl

我想将变量传递给TCL中的awk文件,这与下面的代码有点类似。有什么办法吗?  // out.tr是输入文件,要传递的变量是to_node my_code.tcl:

set to_node 13
exec awk -f check_ack.awk out.tr $to_node

check_ack.awk:

  BEGIN {}
    {
    # variable passed should be saved in node[i] --> But this doesnot work
    for ( i = 0; i < ARGC; i++ ) {
           node[i] = ARGV[i]
       }
    status = $7
        if(FILENAME=="trace.tr") {
            if(node[1] > -1 && $1  == "s" node[1] == $3 && status == "ack") {
            print "Node" " "node[1]" " "has not sent acknowledgement" >         "failed.txt"
            }
        }
    }
    END {
    exit 0
    }

1 个答案:

答案 0 :(得分:2)

你传递这样的变量:

awk -f some/script.awk -v var="my value"

现在您可以在awk中访问变量var,如果您打印它,您会看到它包含my value