Grepping Array Elements from a Tcl File

Grepping Array Elements from a Tcl File

I have something like below in a tcl file.

array set myports {
a
b
c
d
e
}

array set myports {
g
h
i
j
K
l
}

How to do a simple grep on the file and print the elements in the array ?

2

2 Answers

How about this awk command:

awk '/array set myports {/ {for (i=5; i<=NF; i++) {if ($i == "}") {break} else {printf("%s ", $i)}}} END {printf("\n")}' test.in

Hope this helps

1

If it's OK to execute the Tcl file, then you could run a little wrapper Tcl script to extract the values:

$ cat file.tcl
array set A { hello world how are you today }

$ values=$( tclsh <<END_TCL
    source file.tcl
    parray A      
END_TCL
)

$ echo "$values"
A(hello) = world
A(how)   = are
A(you)   = today

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.