GNU awk FS Regular Expression Behavior with Variable-Length Delimiters
23.5K reputation · 17 Apr 2022, 20:04 UTC
GNU awk (gawk) allows the Field Splitter (FS) to be defined as a regular expression, enabling the division of records based on complex patterns rather than single characters. This is particularly useful when processing logs with inconsistent whitespace or multi-character delimiters.
A design uncertainty arises when FS is set to a regex that matches variable-length strings. While gawk identifies the fields based on the pattern, the interaction between the NF (Number of Fields) calculation and the exact boundary of a greedy regex match can vary depending on the input stream's structure.
What is the deterministic behavior of FS when a regular expression matches an empty string or overlapping patterns? How does gawk handle the field indexing when the delimiter regex results in zero-width matches between characters?