SSH and AWK Scripting

I am trying to put an awk command in ssh, for example:

ERRCOUNT=`ssh -n $HOST "ps -ef | grep .job | grep -v grep | grep -v alert_jobs_still_running |wc -l"`

From korn shell prompt this works:

awk '/^Jul 12 16/ {print $0}' /u01/app/oracle/jobs/adhoc/test.dat | wc -l

My data file: /u01/app/oracle/jobs/adhoc/test.dat:

Jul 12 16:13:59 spu76 fctl: [ID 517869 scsi.warning] WARNING: fp(1)::1e0a00 NS failure pkt state=dreason=9, expln=1, NSCMD=0112, NSRSP=8001
Jul 14 11:13:59 spu76 fctl: [ID 517869 scsi.warning] WARNING: fp(1)::GPN_ID for D_ID=1e0200 failed
Jul 12 17:43:59 spu76 fctl: [ID 517869 kern.warning] WARNING: fp(1)::N_x Port with D_ID=1e0200, PWWN=202200a0b8482ab8 disappeared from fabric
Jul 12 14:13:59 spu76 fctl: [ID 517869 kern.warning] WARNING: fp(0)::140a00 NS failure pkt state=dreason=9, expln=1, NSCMD=0112, NSRSP=8001

In a script test2.bat:

#!/bin/ksh
AWKCOUNT=0
HOST=spu76
 
echo "Plain SSH"
ssh -n $HOST "awk '/^Jul 12 16/ {print $0}' /u01/app/oracle/jobs/adhoc/test.dat"
 
echo "AWKCOUNT"
AWKCOUNT=`ssh -n $HOST "awk '/^Jul 12 16/ {print $0}' /u01/app/oracle/jobs/adhoc/test.dat | wc -l"`

I get errors:

(oracle@spu76) /u01/app/oracle/jobs/adhoc > ./test2.bat
Plain SSH
awk: syntax error near line 1
awk: illegal statement near line 1
AWKCOUNT
awk: syntax error near line 1
awk: illegal statement near line 1
(oracle@spu76) /u01/app/oracle/jobs/adhoc >

Ultimately, I want to run this awk statement using ssh:

test4.bat:

#!/bin/ksh
MONTH=`date | awk '{print $2}'`
DAY=`date '+%d`
HOUR=`date +"%H"`
echo "MON: $MONTH DAY: $DAY HOUR: $HOUR"
AWKCOUNT=0
 
AWKCOUNT=`awk -F: '/^'"$MONTH $DAY $HOUR"'/ {print $0} ' /u01/app/oracle/jobs/adhoc/test.dat | awk -F" " '/scsi/ {print $0}' | wc -l`
echo "AWKCOUNT is $AWKCOUNT"

Output:

(oracle@spu76) /u01/app/oracle/jobs/adhoc > ./test4.bat
MON: Jul DAY: 14 HOUR: 11
AWKCOUNT is 1

I have tried many variation of single and double quotes, and escaping characters. Any assistance would be appreciated!

Diane

For something that complicated in backticks, I'd forget the quoting and put it in a separate file:

RESULT=`ssh $HOST exec /bin/sh -s PARAM1 PARAM2 < local-file`

where local-file would be an ordinary script file with no special quoting.

If you need to pass variables into the script, PARAM1 will be available as $1 in the remote script, PARAM2 as $2, etc, etc.

You could also use a here-document like so:

STUFF=`ssh $HOST exec /bin/sh -s PARAM1 PARAM2 <<"EOF"
echo "PARAM1 is $1 PARAM2 is $2"
EOF
`

You should be able to put anything you want between <<"EOF" and EOF unmolested as long as none of the lines inbetween start with EOF.

That final EOF line should be on the beginning of the line, no whitespace in front.

---------- Post updated at 01:07 PM ---------- Previous update was at 12:29 PM ----------

Also, for test4.bat, instead of running 3 separate date instances and an awk instance, why not just do date +"MON: %m DAY: %d HOUR: %H" to print what you want from the beginning?

If you're doing lots and lots of separate ssh instances, it may be smart to do everything in just one, which will save lots of time otherwise wasted connecting, disconnecting, and exchanging keys...

Thank you! I was able to get your example one to work!

I couldn't figure out how to get Jul instead of 07 using date formats. Also, where would I embed the date +"MON: %m DAY: %d HOUR: %H"?
I tried:

AWKCOUNT=`ssh spu76 exec /bin/ksh -s Jul 14 15 < /u01/app/oracle/jobs/adhoc/test8.bat`

.. which worked...but I couldn't get the following to work:

AWKCOUNT=`ssh spu76 exec /bin/ksh -s `date +"%m %d %H"` < /u01/app/oracle/jobs/adhoc/test8.bat`

test8.bat:

awk -F: '/^'"$1 $2 $3"'/ {print $0} ' /u01/app/oracle/jobs/adhoc/test.dat | awk -F" " '/scsi/ {print $0}' | wc -l

Thanks!