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 ?
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
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
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
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 likemyports(g) = h
and so on. What exactly is the output you are expecting?