Rather than testing over ssh, you can replicate the behaviour using eval. I made a test file (called month):
Mar line1 line2 line2 Apr line3
You have (at least) three options:
First option
Your two options are mutually exclusive, so you can sidestep the issue of escaping a ! entirely by using two blocks with next in the first block:
eval "awk '/^Mar|^Apr/ { printf(\"\\n%s\",\$0); next } { printf(\"%s\", \$0) }' month"
If the condition is true, the first block is taken and next skips the rest. Note that I have removed the unnecessary $0 ~ from the condition. The match is performed against the whole line by default.
Second option
You could actually just do this:
eval "awk '/^Mar|^Apr/ { \$0 = \"\\n\"\$0 } { printf(\"%s\", \$0) }' month"
- If the line matches, precede it with a newline.
- In all cases (no condition before the
{ }), print the line.
Third option
If you wrap the overall command in single quotes, you don't need to do anything fancy with the !:
eval 'awk "{ if(/^Mar|^Apr/) { printf(\"\\n%s\",\$0) } if(!/^Mar|^Apr/) { printf(\"%s\", \$0)} }" month'
I recommend one of the other two solutions, I just thought that it would be worth showing that you can use ! within the command if you need to.
Output for all three cases:
Mar line1line2 line2 Apr line3
set -Hto disable it.set -HI still getbash: !~: event not found. One solution is to use single quotes round the whole thing (as shown in my answer).set +H.