B.3.1.3 Using gawk on PC Operating Systems

Information in this section applies to the MinGW port of gawk. See Using gawk In The Cygwin Environment for information about the Cygwin port.

Under MS-Windows, the MinGW environment supports both the ‘|&’ operator and TCP/IP networking (see Using gawk for Network Programming).

The MS-Windows version of gawk searches for program files as described in The AWKPATH Environment Variable. However, semicolons (rather than colons) separate elements in the AWKPATH variable. If AWKPATH is not set or is empty, then the default search path is ‘.;c:/lib/awk;c:/gnu/lib/awk’.

Under MS-Windows, gawk (and many other text programs) silently translates end-of-line ‘\r\n’ to ‘\n’ on input and ‘\n’ to ‘\r\n’ on output. A special BINMODE variable (c.e.) allows control over these translations and is interpreted as follows:

The modes for standard input and standard output are set one time only (after the command line is read, but before processing any of the awk program). Setting BINMODE for standard input or standard output is accomplished by using an appropriate ‘-v BINMODE=N’ option on the command line. BINMODE is set at the time a file or pipe is opened and cannot be changed midstream.

On POSIX-compatible systems, this variable’s value has no effect. Thus, if you think your program will run on multiple different systems and that you may need to use BINMODE, you should simply set it (in the program or on the command line) unconditionally, and not worry about the operating system on which your program is running.

The name BINMODE was chosen to match mawk (see Other Freely Available awk Implementations). mawk and gawk handle BINMODE similarly; however, mawk adds a ‘-W BINMODE=N’ option and an environment variable that can set BINMODE, RS, and ORS. The files binmode[1-3].awk (under gnu/lib/awk in some of the prepared binary distributions) have been chosen to match mawk’s ‘-W BINMODE=N’ option. These can be changed or discarded; in particular, the setting of RS giving the fewest “surprises” is open to debate. mawk uses ‘RS = "\r\n"’ if binary mode is set on read, which is appropriate for files with the MS-DOS-style end-of-line.

To illustrate, the following examples set binary mode on writes for standard output and other files, and set ORS as the “usual” MS-DOS-style end-of-line:

gawk -v BINMODE=2 -v ORS="\r\n" ...


gawk -v BINMODE=w -f binmode2.awk ...

These give the same result as the ‘-W BINMODE=2’ option in mawk. The following changes the record separator to "\r\n" and sets binary mode on reads, but does not affect the mode on standard input:

gawk -v RS="\r\n" -e "BEGIN { BINMODE = 1 }" ...


gawk -f binmode1.awk ...

With proper quoting, in the first example the setting of RS can be moved into the BEGIN rule.