i am getting problem in using AWK command in TCK/TK unix command:

var=`awk -v var1="^$line" -F "|" '$1~var1{print $1 }' sort-address-name-ip.txt | awk -v var2="$line$" '$0~var2'`

and i used as:

set var5 [exec /usr/bin/awk -v var1={^$line} -F {|} {$1~var1{print $1 }} sort-address-name-ip.txt | /usr/bin/awk  -v var2={$line$} {$0~var2}]

my script:

set file [ open /home/gcngov/script/practice/tcl/policy-unique_single_line-src-dst-ip.txt]
while {[gets $file line] != -1} {
        puts $line
        set file1 [open /home/gcngov/script/practice/tcl/sort-address-name-ip.txt]
        set var5 [exec /usr/bin/awk -v var1={^$line} -F {|} {$1~var1{print $1 }} sort-address-name-ip.txt | /usr/bin/awk  -v var2={$line$} {$0~var2}]
        puts $var5

close $file


~/script/practice/tcl$ expect -d while-file.sh 
expect version 5.45
argv[0] = expect  argv[1] = -d  argv[2] = while-file.sh  
set argc 0
set argv0 "while-file.sh"
set argv ""
executing commands from command file while-file.sh 
missing close-bracket
    while executing
"set var5 ["
    ("while" body line 5)
    invoked from within
"while {[gets $file line] != -1} {
        puts $line
        #set var [exec /usr/bin/awk -v var1="^$line" -F "|" $1~var1{print $1 } sort-address-name-ip.txt | awk..."
    (file "while-file.sh" line 3)
  • despite the @msp9011 edit, this still makes no sense. can the OP please fix the question?
    – icarus
    Commented Sep 9, 2019 at 6:02
  • At least fix the missing ] at the end of the line starting set var5.
    – icarus
    Commented Sep 9, 2019 at 6:04
  • 2
    I highly recommend using perl and perl's Expect.pm module instead of expect+sh+awk. All of the features of perl (a superset of awk and sed and sh and lots more) plus expect. in one scripting language, No need to shell out to an external program or mess around with argument quoting.
    – cas
    Commented Sep 9, 2019 at 6:59
  • You have an extra exec in there. That should look like set var5 [exec awk ... | awk ...] ie no exec after the pipe |. You should check the exec(3tcl) manpage. Also, all that could be done in tcl -- no need for external commands.
    – user313992
    Commented Sep 9, 2019 at 7:20
  • 1
    Please give an example of the input file and an example of what the output would look like. Maybe that will help me to understand what this is trying to do. As it is, I don't see why you need TCL at all.
    – hackerb9
    Commented Sep 9, 2019 at 7:59

1 Answer 1


In Tcl, braces are like the shell's single quotes: no variable substitution is performed. You need to change

# to

and similarly for the 2nd awk.

However, I don't see why you're using 2 awks in a pipeline: it looks like you want to find the IPs from policy-unique_single_line-src-dst-ip.txt that appear in column 1 of pipe-delimited sort-address-name-ip.txt :

set ips [exec cut {-d|} -f1 sort-address-name-ip.txt | grep -Fxf policy-unique_single_line-src-dst-ip.txt]
  • Actually i am trying to get exact match from one file result to another with multiple awk command to find exact value and then do some task. eg. one awk with ^$line and another awk with $line\$
    – gcngov
    Commented Sep 13, 2019 at 5:42

You must log in to answer this question.

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