0

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
  • 1
    Doesn't array set create an associative array out of pairs of elements anyhow? So the first assignment is invalid (since it has an odd number of list elements) I think, while the second would produce an array with elements like myports(g) = h and so on. What exactly is the output you are expecting? Oct 4, 2018 at 14:50
  • @sanjoy, please describe your situation in more detail. Why does a shell script need to know about some values in a Tcl file? Oct 4, 2018 at 14:52

2 Answers 2

0

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
  • You'll need to count open/close braces: array set foo { { bar baz } { hello world } } creates an associative array mapping the key " bar baz " to the value " hello world " Oct 4, 2018 at 14:51
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .