awk trouble inside another command

I tried running this.

dsh -w server1 'lsof /audit | awk '{ print $2 }''

It did not like above so I tried to escape the single parenthesis at the end.

dsh -w server1 'lsof /audit | awk '{ print $2 }\''

It then hung so I changed up the parenthesis to this. This worked.

dsh -w server1 "lsof /audit | awk '{ print $2 }'"

I then noticed I needed to add some field separators. This is not working all. I need both colon space, and space all by its self.

dsh -w server1 "lsof /audit | awk 'BEGIN { FS=":" } { print $2 }'"
syntax error The source line is 1.
The error context is
                >>> { <<<
awk: The statement cannot be correctly parsed.

After I get the above to work I like to do this and add the field separator.

dsh -w server1 "kill $(lsof /audit | awk '{print $2}')"

I've stayed away because I could not get past the dsh -w
AIX dsh and linux dash (sometimes dsh) do not have a -w option. Per manpages for both. What does that do -- dsh -w ?

So I'm not sure what is going on. Plus the whole code design concept seems to me to be based on 'one-liner' thinking. Which is cool but not always maintainable as you are finding out.
It looks like you are trying to kill processes with files open - somewhere special. What OS and shell are you using?

I am using AIX. Normally dsh runs a command against all of your servers in your server list. When you add the -w option to dsh it allows you to select which server you want to run your command on. In my case I want to run my command on server1. I could run my command without the -w option but it would be harder to debug my problem.

I am not very good at using multiple single and double quotes, and escaping. I think that is my issue.

Ok, thanks.

Try this to see if it is working the way you want -- before you try killing processes:

dsh -w server1 "lsof /audit | awk '{print $2}' | while read pid; do; echo $pid ; done"

Next try this to see the result. Your process may not have the permissions it needs to kill other processes. I can't know.

dsh -w server1 "lsof /audit | awk '{print $2}' | while read pid; do; kill $pid ; done"

For stuff like this -- In general I usually write a shell script that does what I want. Then I scp a copy to each server; chmod +x the script, then use ssh (similar to dsh) to invoke my script as required on each server. That way I do not get so many single-double quote issues.
on the server.

Would

 ... | awk -F\: '{print $2}')"

work? Mayhap with a double \\ .

The problem is that the remote code runs within quotes, requiring extra escapes and escape-escapes. Try to run more locally

dsh -w server1 'lsof /audit' | awk '{ print $2 }'

Now the pipe and its right side are outside the quotes and run locally. The drawback is more load on the network.

If the only purpose is "kill" then try "fuser -k"

dsh -w server1 "fuser -k /audit"