I'm new to Tcl and UNIX programming.

The following command works on the UNIX command-lines, but doesn't work in a Tcl script.

$ sort -b -k8  abc.txt | awk '!a[$1]++'
error out for 8 (-k8) and 1($1) is not variable

I'm trying to sort on Column 8 first and then pick only unique data on column 1.

I'm also trying to save data into a file.

  • Can you update your question with the source code of the tcl script, 4 spaces at start of line to format code block correctly.
    – X Tian
    Commented Jun 22, 2015 at 14:37
  • Thanks Tian send_unix_cmd "sort -k$sort_col top_$host | awk '!a[${$uni_col}]++' " where sort_col set to 8 and uni_col set to 1
    – user120451
    Commented Jun 22, 2015 at 15:12

2 Answers 2


I think you need to escape [] and $ in the string, eg:

send_unix_cmd "sort -k$sort_col top_$host | awk '!a\[\$$uni_col\]++' "

I also removed the {}.


I thing for executing command in Tcl script you should use exec. And single quotes have no special meaning in Tcl script so it will try to expand the variable $1. You can try following:

exec sort -b -k8  abc.txt | awk {!a[$1]++} > output

If you don't redirect the standard output of the program, it becomes the return value of the exec command, so you can capture it for example:

set result [exec sort -b -k8  abc.txt | awk {!a[$1]++}]

You must log in to answer this question.

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