$ echo "a b c" | awk 'BEGIN {OFS=","}; {print $0};' -
a b c
I was trying to see if OFS applies after the last field, so expecting the output to be either
a,b,c
or
a,b,c,
but the change of OFS doesn't work. Why is it?
You should change/set a field, so that the $0 is recomputed, then OFS will be applied. E.g.
echo "a b c" | awk 'BEGIN {OFS=","}; {$1=$1;print $0};'
{ print $1, $2, $3 } will use the OFS value regardless of any fields got updated or not. But this solution is not portable. I am hoping other better solutions such as print $WITH_OFS that may be a new feature of AWK.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With