> [...]
> >> RESULT=`ssh ${host} " awk < $file ' /ENVIRON[\"criteria_ENV\"]/ && FNR
> >>>1 { print last_line } { last_line = $0 }' "`
> >> I have also tried exporting within the ssh:
> >> RESULT=`ssh ${host} " export criteria_ENV ; awk < $file '
> >> /ENVIRON[\"criteria_ENV\"]/ && FNR > 1 { print last_line } { last_line
> >> = $0 }' "`
> [...]
> But, here, ssh comes in the way, which makes things more
> complicated.
> The problem with ssh is that it concatenates the arguments to
> build a string interpreted as a command line by the remote
> shell.
> Which means that
> ssh remote-host echo 'foo; rm -rf /'
> Only outputs "foo".
> So you need to transform the variables so that they are taken as
> is.
> Something like:
> ssh remote-host '
> remote_shell_var="expanded value of $local_shell_var"
> awk whatever solution in the FAQ'
> One way to do this can be something like:
> eval "$(
> awk '
> BEGIN {
> for (i=1; i<ARGC; i++) {
> a=ARGV[i]
> gsub(/'\''/, "'\'\\\\\'\''", a)
> a="'\''" a "'\''"
> gsub(/'\''/, "'\'\\\\\'\''", a)
> print "a" i "='\''" a "'\''"
> }
> exit
> }' "$criteria" "$file")"
> # now $a1 contains the quoted version of $criteria
> # and $a2 contains the quoted version of $file
> ssh remote-host "
> criteria=$a1
> file=$a2
> export criteria
> awk '
> \$0 ~ ENVIRON[\"criteria\"] && NR > 1 {print last}
> {last = $0}' < \"\$file\"}'"
> (untested).
> That quoting mess can get very confusing. ssh is to blame as it
> doesn't provide any way to provide the remote sh with additional
> arguments.
Thanks for your exhaustive replies chaps, I appreciate it. For one I'm
on a learning curve and two it's just kind of you anyway!
I tried to understand the first block of code but I must admit I
haven't grasped it yet. So I literally took what you posted and gave
it a try. I received the following error:
A file or directory in the path name does not exist.
/usr/bin/ksh[5]: /tmp/file}: 0403-016 Cannot find or open the file.
The 'file' is in the /tmp directory but notice the extra curly brace at
the end, so obviously the shell is looking for /tmp/file} rather than
/tmp/file. I did notice what seems to be an extra curly brace at the
end of < \"\$file\"}'" and attempted to remove it but then ran in to
further errors. So I stopped, I'm going to try and understand why this
is breaking (since I'm new to awak and shell programming) before I just
randomly attempt remove stuff to figure it out.