gawk: DOS Quoting
1
1 1.1.6.1 Quoting in MS-Windows Batch Files
1 .........................................
1
1 Although this Info file generally only worries about POSIX systems and
1 the POSIX shell, the following issue arises often enough for many users
1 that it is worth addressing.
1
1 The "shells" on Microsoft Windows systems use the double-quote
1 character for quoting, and make it difficult or impossible to include an
1 escaped double-quote character in a command-line script. The following
1 example, courtesy of Jeroen Brink, shows how to escape the double quotes
1 from this one liner script that prints all lines in a file surrounded by
1 double quotes:
1
1 { print "\"" $0 "\"" }
1
1 In an MS-Windows command-line the one-liner script above may be passed
1 as follows:
1
1 gawk "{ print \"\042\" $0 \"\042\" }" FILE
1
1 In this example the '\042' is the octal code for a double-quote;
1 'gawk' converts it into a real double-quote for output by the 'print'
1 statement.
1
1 In MS-Windows escaping double-quotes is a little tricky because you
1 use backslashes to escape double-quotes, but backslashes themselves are
1 not escaped in the usual way; indeed they are either duplicated or not,
1 depending upon whether there is a subsequent double-quote. The
1 MS-Windows rule for double-quoting a string is the following:
1
1 1. For each double quote in the original string, let N be the number
1 of backslash(es) before it, N might be zero. Replace these N
1 backslash(es) by 2*N+1 backslash(es)
1
1 2. Let N be the number of backslash(es) tailing the original string, N
1 might be zero. Replace these N backslash(es) by 2*N backslash(es)
1
1 3. Surround the resulting string by double-quotes.
1
1 So to double-quote the one-liner script '{ print "\"" $0 "\"" }' from
1 the previous example you would do it this way:
1
1 gawk "{ print \"\\\"\" $0 \"\\\"\" }" FILE
1
1 However, the use of '\042' instead of '\\\"' is also possible and easier
1 to read, because backslashes that are not followed by a double-quote
1 don't need duplication.
1