0

I try to run a remote awk statement using ssh. My code is:

ssh username@hostIP "awk 'NR==1  {max=0;min=1} NR>1 {if (max<\$3) max=\$3}END {print max}' FS=\",\" /path_to_my_file"

When I run this command, I receive no error message, but command is not working and doesn't produce an output but just hanging so I would need to cancel it by ctrl+c.

Is there something I am missing?

JavaRed
  • 708
  • 4
  • 10
  • 34
  • what is that `FS=` doing there after the `awk` command? Maybe you meant to do `awk -v FS=, '...'` instead? – janos Nov 15 '13 at 22:06
  • janos is right, I think double quote after FS is confusing ssh whereas I used \ to escape them, but it looks it didn't work. With the format like ssh username@hostIP "awk -v FS=, 'NR==1 {max=0;min=1} NR>1 {if (max<\$3) max=\$3}END {print max}' /path_to_file " the command produced desired result. Thanks. – JavaRed Nov 15 '13 at 22:39

1 Answers1

10

Try this using a here-doc :

ssh -t username@hostIP <<'EOF'
awk '
    NR==1 {max=0;min=1}
    NR>1 {if (max<$3) max=3}
    END {print max}
' FS="," /path_to_my_file
EOF

The single quotes around EOF prevent shell expansion.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223