0

I have a below values in file.

user1 India GJ 1000 IT contractor
user2 Ind GJ 1001 HR contractor
user3 USA CA 1002 R&D hourly
user4 India TN 1003 R&D fte
user5 Ind KA 1004 FE fte
user6 In GJ 2345 HR hourly

Need to match 3rd column and update 2nd column. I am able to do using below command.

awk -i inplace '/GJ|TN|KA/ {$2="IN"} {print}' filename

output for above command is

user1 IN GJ 1000 IT contractor
user2 IN GJ 1001 HR contractor
user3 USA CA 1002 R&D hourly
user4 IN TN 1003 R&D fte
user1 IN GJ 1000 IT contractor
user5 IN KA 1004 FE fte
user6 IN GJ 2345 HR hourly

But the same does not work when I run over ssh

ssh -q server1 "awk -i inplace '/GJ|TN|KA/ {\$2="IN"} {print}" filename

The output over SSH

user1  GJ 1000 IT contractor
user2  GJ 1001 HR contractor
user3 USA CA 1002 R&D hourly
user4  TN 1003 R&D fte
user1  GJ 1000 IT contractor
user5  KA 1004 FE fte
user6  GJ 2345 HR hourly

not sure what I am missing and why its not working our SSH. I am open for any other solutions.

Thanks.

KumarJohn
  • 427
  • 3
  • 11
  • 21

1 Answers1

3

What happened

Your command

ssh -q server1 "awk -i inplace '/GJ|TN|KA/ {\$2="IN"} {print}'" filename

(note I added the missing '; without it awk would not run at all)

does not do what you want because the local shell removes all the double-quotes. Look:

          ssh -q server1 "awk -i inplace '/GJ|TN|KA/ {\$2="IN"} {print}'" filename
quoted:                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^    ^^^^^^^^^^
unquoted: ^^^^^^^^^^^^^^^                                  ^^            ^^^^^^^^^

In effect ssh gets awk -i inplace '/GJ|TN|KA/ {$2=IN} {print}' and filename (as separate arguments). It builds the following command to run on the remote side:

awk -i inplace '/GJ|TN|KA/ {$2=IN} {print}' filename

This means awk executes

/GJ|TN|KA/ {$2=IN} {print}

where IN is not considered to be a string, it's a variable in awk. The variable is empty.


Solution

To make the "inner" double-quotes survive and get to the remote shell (and to awk eventually), escape them just like you did with $:

ssh -q server1 "awk -i inplace '/GJ|TN|KA/ {\$2=\"IN\"} {print}'" filename

More information: