The awk manual indicates that both -v FS
and -F
are equivalent ways to set the field separator.
The GNU Awk User’s Guide -> 4.5.4 Setting FS from the Command Line:
FS can be set on the command line. You use the `-F' argument to do so.
(...)
The value used for the argument to `-F' is processed in exactly the
same way as assignments to the built-in variable FS.
However, I noticed that there is a difference if we set it to an empty string, it is not the same. Tested on my GNU Awk 4.1.1
.
This works:
$ awk -F, '{print $2}' <<< "a,b,c"
b
$ awk -v FS=, '{print $2}' <<< "a,b,c"
b
But this does not:
$ awk -F="" '{print $2}' <<< "abc"
# $1 contains abc
$ awk -v FS="" '{print $2}' <<< "abc"
b
Why? Is this because setting FS
to empty is a gawk
specific?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…