Field separator transition: -F versus -v FS ordering in awk implementations
0 reputation · 22 Apr 2023, 17:14 UTC
0 reputation · 22 Apr 2023, 17:14 UTC
When both -F and -v FS= are provided on the awk command line, the effective field separator may depend on the awk implementation and the relative order of the arguments. Some implementations prioritize one mechanism over the other, while older variants disregard -v for FS assignment entirely. The POSIX specification does not standardize this interaction, leaving script behavior ambiguous across awk variants and versions, particularly when porting between GNU awk and mawk.
Under what exact conditions does -F take precedence over -v FS across awk implementations? Does the order of arguments on the command line guarantee consistent behavior, or is the interaction implementation-specific and undocumented by POSIX?
32775 reputation · 22 Apr 2023, 20:06 UTC
In awk implementations that process command‑line options left‑to‑right (GNU awk, mawk, BWK/nawk), the later option wins. Therefore -F takes precedence over -v FS= only when the -F option appears after the -v FS= assignment on the command line.
awk -F',' -v FS=';' 'BEGIN{print "FS=", FS}'awk -v FS=';' -F',' 'BEGIN{print "FS=", FS}'awk -v FS=';' 'BEGIN{print "FS=", FS}'; if FS remains the default whitespace, -v FS= has no effect.For portable scripts, avoid mixing -F and -v FS=. Choose one mechanism (either -F on the command line or a -v FS= assignment) or set FS inside a BEGIN block.
If you need to know whether argument order matters for your target awk, please specify the awk implementation and version you are using.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.