I know that default FS= " ", then why am i seeing variations in following awk commands. Please help me understand.
>echo " ABC DEF XYZ \n abc def,ghi xyz \n" | awk '{printf("nf: %s 1:%s line: %s\n", NF, $1, $0)}'
nf: 3 1:ABC line: ABC DEF XYZ
nf: 3 1:abc line: abc def,ghi xyz
nf: 0 1: line:
>echo " ABC DEF XYZ \n abc def,ghi xyz \n" | awk -F" " '{printf("nf: %s 1:%s line: %s\n", NF, $1, $0)}'
nf: 3 1:ABC line: ABC DEF XYZ
nf: 3 1:abc line: abc def,ghi xyz
nf: 0 1: line:
>echo " ABC DEF XYZ \n abc def,ghi xyz \n" | awk -F"[ ]" '{printf("nf: %s 1:%s line: %s\n", NF, $1, $0)}'
nf: 10 1: line: ABC DEF XYZ
nf: 17 1: line: abc def,ghi xyz
nf: 0 1: line:
>echo " ABC DEF XYZ \n abc def,ghi xyz \n" | awk -F"[ ]*" '{printf("nf: %s 1:%s line: %s\n", NF, $1, $0)}'
nf: 5 1: line: ABC DEF XYZ
nf: 5 1: line: abc def,ghi xyz
nf: 0 1: line:
I want to understand why there are no empty tokens in 1st & 2nd examples, but exists in 3rd & 4th examples.
Update: To explain my doubt further, awk behaves inconsistently with default FS and custom FS. See below examples.
>printf "ab cd\nef gh\n" | awk -F" " '{ printf("nf: %d\t", NF); for (i=1;i<=NF;i++) printf("%02d:%s\t", i, $i); print ""}'
nf: 2 01:ab 02:cd
nf: 2 01:ef 02:gh
>printf "ab::cd\nef:gh\n" | awk -F":" '{ printf("nf: %d\t", NF); for (i=1;i<=NF;i++) printf("%02d:%s\t", i, $i); print ""}'
nf: 3 01:ab 02: 03:cd
nf: 2 01:ef 02:gh