gawk versus mawk: BEGIN block handling of empty piped input
0 reputation · 13 Jun 2022, 04:18 UTC
This inquiry examines how two major awk implementations gawk and mawk treat the execution sequence of BEGIN and END blocks when input is empty or when no records match the pattern-action rule. The objective is to document whether the pattern-action block is uniformly suppressed across both runtimes or if divergent handling introduces portability risk for scripts that depend on deterministic initialization or summary output.
Research confirms that awk invokes BEGIN before the first record and END after the last yet the behavior when no records exist varies between implementations. The FS and RS variable scoping as well as the presence of piped input from /dev/null or empty streams can influence which code paths are taken. No cross-implementation specification mandates identical skipping of the pattern-action block leaving script authors to contend with undocumented differences.
- Does gawk execute the pattern-action block when stdin is empty while mawk skips it entirely?
- How does the presence of a BEGIN block that redefines FS affect the record-reading loop in each implementation?
- Are there flags or environment variables that normalize BEGIN END execution order across gawk and mawk?