How to preserve gawk in ssh?

I am trying to extract some information out of a policy listing command. I have the following:

for server in test1 test2; do ssh $server '/usr/listinfo policy -L |gawk 'NR == 2'';done

This runs without the awk and sshs to remote server, but with the gawk then I get gawk command not found. How do I make sure the gawk is picked up? Is there a way to avoid the pipe?

This has two problems.

First and foremost, you cannot nest single quotes in single quotes. You can't escape anything in single quotes either, so not even backslashes will fix it. You'll have to put double quotes on the outside.

Second, not all systems have gawk. In fact very few systems have gawk; even systems like Linux which default to it just call it awk.

Your awk command is simple enough that hopefully vanilla awk can handle it, even the awful old versions some systems have.

for server in test1 test2; do ssh $server "/usr/listinfo policy -L | awk 'NR == 2'";done