简体   繁体   中英

Escaping nested double quotes in awk

In a Windows Server environment, having trouble with escaping quotes that are within quotes. Using the functioning batch code below as a base:

awk -F "|" "$4 ~ /JUVENILE/ {sum +=$6} END {printf sum}" sourcedata.file

Problems occur when trying to include "%.3f" to printf. I am confused and unable to properly escape these quotes in my environment.

awk -F "|" "$4 ~ /JUVENILE/ {sum +=$6} END {printf """%.3f""", sum}" sourcedata.file

The above will work at the command prompt, but will not work in a simple batch file. I have also attempted to replace "%.3f" with \\"%.3f\\" and \\""%.3f\\"" and these also do not work in a batch file.

The standard advice for Windows to avoid their nightmarish quoting rules is:

a) Don't call the script from Windows. Install cygwin or similar to get a UNIX-like environment and then call the script from that, or...

b) Don't specify the script text on the command line in Windows, save it in a file instead, ie put this in a file named foo.awk :

BEGIN { FS="|" }
$4 ~ /JUVENILE/ {sum +=$6}
END {printf "%.3f", sum}

and then execute it as awk -f foo.awk sourcedata.file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM