Tuesday, July 14, 2009

Text alignment with Awk printf function


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

3 comments:

Mahesh Kharvi said...

Really nice one ...

Mahesh Kharvi said...

Good use of built-in variables.
I usually do something like this.

awk '{ for(i=1;i<=NF;i++) printf("%-15s", $i); printf "\n"}'

Jadu Saikia said...

@Mahesh, thanks for the same.

© Jadu Saikia www.UNIXCL.com