Arthur Accioly
Arthur Accioly

Reputation: 801

How to use SSH to run a local awk script on a remote machine with the remote file name as parameter?

I know that I can run a local bash script on a remote host via ssh per this other question. But what about running a local awk script that receives a file as a parameter?

I've tried something like this but no luck:

ssh myuser@machine 'awk -s' < awk -f my_local_awk_script.awk "/remote/path/remote_file.txt"

Upvotes: 0

Views: 191

Answers (2)

Philippe
Philippe

Reputation: 26677

Can this achieve what you expected :

ssh myuser@machine awk -s -f /dev/stdin /remote/path/remote_file.txt < my_local_awk_script.awk

Upvotes: 2

Paul Hodges
Paul Hodges

Reputation: 15358

Try

ssh myuser@machine "awk -s '$(<my_local_awk_script.awk)' /remote/path/remote_file.txt"

This interpolates the exact contents of the local file as flat text into the string, so embedded single-quotes will likely cause it to explode.

My target system doesn't have the -s option for awk, so I couldn't test that.

My local file:

BEGIN{ print "Hello, " }
NR=1{print "World!"; exit; }

the output:

$: ssh sa-nextgen-jenkins.eng.rr.com "awk '$(<x)' x"
Hello,
World!

Safer - copy the file over and execute it, then delete if you need to.

$: scp x sa-nextgen-jenkins.eng.rr.com:~/x && 
>  ssh sa-nextgen-jenkins.eng.rr.com "awk -f x .bash_profile && rm ~/x"
x                                                                                                              
100%   56     0.7KB/s   00:00
Hello,
World!

so for you:

scp my_local_awk_script.awk myuser@machine:~/my_local_awk_script.awk &&
ssh myuser@machine "awk -s -f ~/<my_local_awk_script.awk /remote/path/remote_file.txt && rm ~/my_local_awk_script.awk"

Make sure you don't botch that and accidentally delete your local. You might want to put the remote in /tmp, etc.

Upvotes: 2

Related Questions