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 ?

steeldriver
  • 136,215
  • 21
  • 243
  • 336
  • 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? – steeldriver Oct 04 '18 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? – glenn jackman Oct 04 '18 at 14:52

2 Answers2

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

Lewis M
  • 755
  • 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 " – glenn jackman Oct 04 '18 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
glenn jackman
  • 17,900