formatting

I have file with different columns
for ex.

contents of file "txt"

NAME AGE MARKS
HARRY 23 89
TOM 12 67
BOB 23 11

and you see its not formatted.Now, I need the file "txt" to be formatted like

COLUMN1 COLUMN2 COLUMN3
NAME AGE MARKS
with proper spacing between them...

how to do this in shell scripting?

The fast way will be to replace space by tab's.

tr ' ' '\t' <  file

A more general solution is to use printf

while read name age marks; do
  printf "%-12s %2i %s\n" "$name" "$age" "$marks"
done <txt

It's simpler still if you use awk, because it will loop over each line by itself:

awk '{ printf "%-12s %2i %s\n", $1, $2, $3 }' txt

The widths (here, 12, 2, and unbounded) are obviously up to you to set to suitable values.

Hi.

A production-quality perl script for automatically aligning columns can be found at align: text column alignment filter, written by Steve Kinzler.

Although the title is text column ..., it right-adjusts if a field is numeric. There are command-line over-ride options for adjustment direction, along with numerous other features. I find it to be a very useful code ... cheers, drl