Behavior: awk multi‑character field separator differences across POSIX implementations
0 reputation · 22 Oct 2022, 04:44 UTC
0 reputation · 22 Oct 2022, 04:44 UTC
When restoring backup data with awk scripts that rely on field separators, inconsistencies in how multi‑character FS strings are interpreted can break parsing logic. What behavior differences exist between gawk, mawk, and nawk, and how do they affect backup restoration scripts?
gawk and nawk interpret a multi‑character FS as a regular expression, while mawk treats it as a literal character class (any single character from the string). The special whitespace rule FS==" " that collapses runs of spaces/tabs is honoured by gawk and nawk but not by mawk.
The POSIX awk specification does not define how a FS longer than one character should be used, leaving the interpretation to each implementation. This ambiguity leads to the observed divergence.
FS with >1 char is a regex (e.g., FS="\t+" splits on one or more tabs). FS==" " still triggers whitespace‑run splitting.FS is a regex and the whitespace rule applies.FS is taken literally; if longer than one character it splits on any single character from that set (character class). The FS==" " whitespace optimisation is absent, so a single space acts as a literal separator and consecutive spaces produce empty fields.echo -e 'a||b||c' | gawk -F'||' '{print NF, $1, $2, $3}'
mawk and nawk and compare the field count (NF) and output.echo -e 'a b c' | gawk -F' ' '{print NF, $1, $2, $3}'
mawk and nawk; mawk will report more fields because each space is a literal separator.To give a definitive recommendation, we need to know which awk implementation is invoked by your restore script (e.g., via #!/usr/bin/awk or the system default). If the script runs under mawk, you should replace multi‑character FS with an explicit regex such as FS="[|][|]" or use FS="[ \t]+" for whitespace.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.