Input file is comma separated.
$ cat file.txt
hiddenhausen,99.60,y
herbstein,99.021,n
bangalore,98.82,y
golm,98.8,y
para,98.82,n
bogen,98.61,n
saintandre,98.5,n
delhi,98.61,y
hyderabad,99.02,y
Lets try to format the above file:
$ awk '{ printf "%-15s%-8s%s\n",$1,$2,$3}' FS=\, file.txt
hiddenhausen 99.60 y
herbstein 99.021 n
bangalore 98.82 y
golm 98.8 y
para 98.82 n
bogen 98.61 n
saintandre 98.5 n
delhi 98.61 y
hyderabad 99.02 y
So its printing first field ($1) as a string of 15 characters that are left-justified, 2nd field ($2) as 8 characters left-justified and then third field ($3)
With variable number of fields in each line, a generic solution would be:
$ awk '
{
for(i=1;i<=NF;i++)
printf("%-15s%c", $i, (i==NF) ? ORS : "")
}' FS=, file.txt
hiddenhausen 99.60 y
herbstein 99.021 n
bangalore 98.82 y
golm 98.8 y
para 98.82 n
bogen 98.61 n
saintandre 98.5 n
delhi 98.61 y
hyderabad 99.02 y
Related post:
-Formatting fields into columns in awk
