Align the words

HI ,

I am new to shell scripting i m getting the below format like this

Name                  FirstName            Lastname
------               ---------            ----------
Name1            Balaji                 NandaKishore  
Name123            Vijaya                  krsihna

But I want in the formatted way like as below:

Name                FirstName            Lastname
------              ---------            ----------
Name1               Balaji                 NandaKishore  
Name123            Vijaya                krsihna

please help me to find out the solution.

I'm not able to understand the difference between your input and output. Please elaborate.

hi extremely sorry,

Alignement problem

What i found is??

Name                  FirstName            Lastname
------               ---------            ----------
 Name1            Balaji                 NandaKishore  
Name123            Vijaya                  krsihna

But I want in the formatted way like as below:

  Name            FirstName             Lastname

 -------           --------             ----------
Name1             Balaji                  NandaKishore  
Name123          Vijaya                  krsihna


$ nawk '{ printf "%-8s %-12s %-8s \n" ,$1,$2,$3 }' infile
Name     FirstName    Lastname
------   ---------    ----------
Name1    Balaji       NandaKishore
Name123  Vijaya       krsihna
$

Hi.

The best automatic alignment tool I've found is the perl script align. Here's a demonstration:

#!/usr/bin/env bash

# @(#) s1	Demonstrate automatic alignment, align.
# http://freecode.com/projects/align

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C align

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
align $FILE

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39
align 1.7.0

-----
 Input data file data1:
Name                  FirstName            Lastname
------               ---------            ----------
Name1            Balaji                 NandaKishore  
Name123            Vijaya                  krsihna


-----
 Results:
Name    FirstName Lastname
------  --------- ----------
Name1   Balaji    NandaKishore
Name123 Vijaya    krsihna

Best wishes ... cheers, drl

You could use expand (it usually works with TAB separated input so I used sed to convert your spaces to tabs first):

$ sed 's/  */\t/g' infile | expand -t 12
 
Name        FirstName   Lastname
------      ---------   ----------
Name1       Balaji      NandaKishore            
Name123     Vijaya      krsihna

Replace the 12 with any number you like for the column widths or specify tabstops as a comma list eg -t 12,18,35