awk error in script

Hello Team,

I am using below awk code in the script

 
ssh -o ConnectTimeout=15 -n -l USER SERVER awk -F'\;' '{ if ( $3 == '-' && $1 ~ /[ES]/ ) print }' *20130706*
 

but its giving error as

awk: syntax error near line 1
awk: illegal statement near line 1

Please help.

---------- Post updated at 12:26 AM ---------- Previous update was at 12:24 AM ----------

*20130706* file has following data

S;X;36031;
E;-;-;
S;X;-;

So i want to get second and third line as output

Try:

awk -F'\;' '{ if ( $3 == "-" && $1 ~ /[ES]/ ) print }' *20130706*

i tried the same its giving me same error.

---------- Post updated at 01:40 AM ---------- Previous update was at 01:37 AM ----------

Seems there is some problem with $3 == "-" only as I tried $1 ~ /[ES]/ separately... it workes fine ....

try..

 
ssh -o ConnectTimeout=15 -n -l USER SERVER "awk -F'\;' '{ if ( \$3 == "-" && \$1 ~ /[ES]/ ) print }' *20130706*"

nopes not working :frowning:

---------- Post updated at 02:09 AM ---------- Previous update was at 02:08 AM ----------

thers is some issue in matching hyphen "-" ...

Were you able to execute the awk command alone without the ssh successfully?

awk -F'\;' '{ if ( $3 == "-" && $1 ~ /[ES]/ ) print }' *20130706*

I was able to execute the above successfully. You may then try the below command.

ssh -o ConnectTimeout=15 -n -l USER SERVER $(awk -F'\;' '{ if ( $3 == "-" && $1 ~ /[ES]/ ) print }' *20130706*) 

There is also a problem with the remote command.
It is evaluated once on the calling system and another time on the remote system.
To solve, put the remote command in 'ticks'. And solve the problem with the embedded ticks.

ssh -o ConnectTimeout=15 -n -l USER SERVER 'awk -F\; '"'"'{ if ( $3 == "-" && $1 ~ /[ES]/ ) print }'"'"' *20130706*'

That looks ugly.
Better save the normal command in a file "remotecommand.sh"

#!/bin/sh
awk -F\; '{ if ( $3 == "-" && $1 ~ /[ES]/ ) print }' *20130706*

And send that to the remote server (without -n)

ssh -o ConnectTimeout=15 -l USER SERVER /bin/sh < remotecommand.sh

The script interpreter (/bin/sh) is called explicitly, and the normal command is piped through the ssh to it.
NB: I replaced the '\;' delimiter by a simple \; meaning a semicolon.