Use awk in expect.

I am trying to user awk in expect command. It seems to be failing. Can someone help me correct the syntax.

This is working fine

expect -c 'spawn ssh $env(PUSER)@$env(PHOST1)  "ls -l $env(P_BKP_PATH) | grep -i $env(bktmstmp)"; expect assword ; send "$env(PASSP)\n" ; interact'

When I add an awk to it, it starts failing.

expect -c 'spawn ssh $env(PUSER)@$env(PHOST1)  "ls -l $env(P_BKP_PATH) | grep -i $env(bktmstmp) | awk '{print $5}'"; expect assword ; send "$env(PASSP)\n" ; interact'

No idea about expect. But generally, during multiple program interaction you need to escape the special characters so that they can be ignored by the first program (expect engine in your case).

try awk \'{print $5}\'

You might also need to escape the "{","}" and even "$" if its a keyword in expect
I am not completely sure but you can try this.

That doesn't seem to be working. I tried giving backslash for all special characters.

Hi.

It looks to me like you have:

expect -c 'long-string-of-characters'

then you added:

expect -c 'long-string awk 'awk-command-stuff' of-characters'

however:

       Enclosing characters in single quotes preserves the literal value of
       each character within the quotes.  A single quote may not occur between
       single quotes, even when preceded by a backslash.
-- excerpt from man bash

So you will need to find another method of using an awk program without using the single quotes -- perhaps making it available on the remote machine. Another possibility is to enclose the long string in double quotes. The "embedded" double quotes can be escaped and single quotes would be OK inside double quotes:

expect -c "long-string awk 'awk-command-stuff-like \"$5\"' of-characters"

Note that other special characters may then also need to be escaped.

Best wishes ... cheers, drl

I would appreciate, if you could please give the command replacement for this as per your interpretation

expect -c 'spawn ssh $env(PUSER)@$env(PHOST1) "ls -l $env(P_BKP_PATH) | grep -i $env(bktmstmp) | awk '{print $5}'"; expect assword ; send "$env(PASSP)\n" ; interact'

How about this:

expect -c "spawn ssh \$env(PUSER)@\$env(PHOST1) \"ls -l \$env(P_BKP_PATH) | grep -i \$env(bktmstmp) | awk '{print \$5}'\"; expect assword ; send \"\$env(PASSP)\n\" ; interact"

Things might be simpler if you put the expect code in a file eg:

#!/usr/bin/expect
spawn ssh $env(PUSER)@$env(PHOST1) "ls -l $env(P_BKP_PATH) | grep -i $env(bktmstmp) | awk '{print $5}'"
expect assword 
send "$env(PASSP)\n"
interact

Script in a file works but expect -c still doesn't work.

---------- Post updated at 01:00 AM ---------- Previous update was at 12:52 AM ----------

It would be great if some one could help me out to accomplish this with a single command. That is by using expect -c. I would need to use many of these commands in my script. Giving seperate expect file for each expect command would be a hassle.

Chubler_XL's one line solution should work. Another way to do it is change each of the two single quotes in you original code to

'"'"'

That's sq1 dq sq2 dq sq3. With single quotes, there is no escaping. The only way is to end it with a matching single quote. Here the sq1 and sq3 are to end the previous and start the next part. dq sq2 dq (or you can also use backslash single quote) is the real single quote for the awk.