Awk has a nifty built-in variable users can overwrite called OFS. OFS stands for output field separator.
Pretend I have a file which has 5 columns separated by single spaces, each of which I would like to print via AWK in tab-separated format.
Life without OFS:
cat file.txt | awk -F" " '{print $1"\t"$2"\t"$3"\t"$4"\t"$5}'
Sticking a "\t" between each variable proves to be very annoying when scaled up.
Life with OFS:
cat file.txt | awk -F" " 'BEGIN{OFS="\t";}{print $1,$2,$3,$4,$5}'
This seems a tad more reasonable. There is certainly less typing involved. The example I provided may not be the best use case, but it definitely shows the capability of OFS and where it can be useful.
Pretend I have a file which has 5 columns separated by single spaces, each of which I would like to print via AWK in tab-separated format.
Life without OFS:
cat file.txt | awk -F" " '{print $1"\t"$2"\t"$3"\t"$4"\t"$5}'
Sticking a "\t" between each variable proves to be very annoying when scaled up.
Life with OFS:
cat file.txt | awk -F" " 'BEGIN{OFS="\t";}{print $1,$2,$3,$4,$5}'
This seems a tad more reasonable. There is certainly less typing involved. The example I provided may not be the best use case, but it definitely shows the capability of OFS and where it can be useful.


