Operating System - HP-UX
1832973 Members
2179 Online
110048 Solutions
New Discussion

Text Allignment problem. ? with TAB space.

 
SOLVED
Go to solution
rveri
Super Advisor

Text Allignment problem. ? with TAB space.

Hi All ,


I am Trying to print like this : username , uid , gid , gecos

With this command. But the allignment is getting disturbed.
Can any one tell , how to allign 2nd field , so that it will maintain in same column.??

$ sort -t: -k 3n,3 passwd | awk -F ":" '{ print $1 ,"\t","\t","\t",$3,"\t","\t",$4,"\t",$5}' | more

-------------------------------------------------------------------------

nobody -2 -24
root 0 3
daemon 1 5
bin 2 2
sys 3 3
adm 4 4
uucp 5 3
lp 9 7
nuucp 11 11
hpdb 27 1 ALLBASE
sna 28 21 SNAplus2 User,,,
www 30 1
sybase 100 101 ,,,
oracle 101 101 Oracle DBA,,,
dbaumgr 102 102 ,,,
dptrans 103 22 File transfer,,,
d_mirror 104 101 data mirror,x7103,,
sshd 107 103 sshd privsep
uua332 108 104 User34,username;Location2;ad2
tpcak 109 104 User35, Username2;Location3;ad3

-----------------------------------------------------------------------------

Thanks ,
Veri.
12 REPLIES 12
rveri
Super Advisor

Re: Text Allignment problem. ? with TAB space.

Sorry , in the forum text box the output is not displaying properly ,

here is the attachment , that will show clear about the problem. (can be opened with wordpad. )

Thanks ,
Veri.
Patrick Wallek
Honored Contributor

Re: Text Allignment problem. ? with TAB space.

Use printf instead of just print.

If you want the names left justified:

sort -t: -k 3n,3 passwd | awk -F ":" '{ printf("%-20s,%s,%s,%s\n",$1,$3,$4,$5) }'

If you want the names right justified:
sort -t: -k 3n,3 passwd | awk -F ":" '{ printf("%20s,%s,%s,%s\n",$1,$3,$4,$5) }'

You can change the 20 in %20s to the length of your longest user name.
James R. Ferguson
Acclaimed Contributor

Re: Text Allignment problem. ? with TAB space.

Hi Veri:

# sort -t: -k3n,3 passwd | awk -F ":" '{printf "%12s %4d %4d %s\n",$1,$3,$4,$5}'

...adjusting the widths to suit your tastes/needs.

Regards!

...JRF...


Try something like this:

Rick Garland
Honored Contributor

Re: Text Allignment problem. ? with TAB space.

The various names you get as resuly have different lengths so the tab setting is unreliable

Use the 'printf'
sort -t: -k 3n,3 passwd | awk -F ":" '{ printf
%-9s %-4s %-4s %-25s\n", $1, $3, $4, $5}' | more

You are defining the length of the fields to print. Example, $1 has 9 characters, $3 has 4 characters, etc...


Volker Borowski
Honored Contributor

Re: Text Allignment problem. ? with TAB space.

Hello,
not exactly sure, but

printf "%-20s\t%10s\t%10s\t%s\n", $1,$3,$4,$5 }

should do the job in nice formatting.
Volker
Patrick Wallek
Honored Contributor

Re: Text Allignment problem. ? with TAB space.

If you want more padding between columns (I didn't see your 2nd post until after I originally responded):

sort -t: -k 3n,3 passwd | awk -F ":" '{ printf("%20s %7s %7s %s\n",$1,$3,$4,$5) }'

Change the numbers in the %#s statements to whatever value you want.

man awk

and look at the printf section for more info.
rveri
Super Advisor

Re: Text Allignment problem. ? with TAB space.

Hi All ,

Patrick, James works fine ,
Ricks one gives error :

$ sort -t: -k 3n,3 passwd | awk -F ":" '{ printf %-9s %-4s %-4s %-25s\n", $1, $3, $4, $5}' | more
syntax error The source line is 1.
The error context is
{ printf >>> % <<< -9s %-4s %-4s %-25s\n", $1, $3, $4, $5}
awk: The statement cannot be correctly parsed.
The source line is 1.
$
--------------------------------------------------------------

And Patrick's last one excellent , Patrick, how to make the names left allinged.

Thanks all,
Patrick Wallek
Honored Contributor
Solution

Re: Text Allignment problem. ? with TAB space.

To left align, make the %20s be %-20s and you it should work.

The command would then be:

sort -t: -k 3n,3 passwd | awk -F ":" '{ printf("%-20s %7s %7s %s\n",$1,$3,$4,$5) }'

You can do the same on any of the fields.
rveri
Super Advisor

Re: Text Allignment problem. ? with TAB space.

Thanks Patric , James, Rick , Volker ..

And Thanks all , its working.

rveri
Super Advisor

Re: Text Allignment problem. ? with TAB space.

Got solution. You all are .Excellent.
Rick Garland
Honored Contributor

Re: Text Allignment problem. ? with TAB space.

Sorry rveri, mine is missing the double quotes at the beginning.

rveri
Super Advisor

Re: Text Allignment problem. ? with TAB space.

No Problem ,
Now I understood the syntax and details of %s with printf , and worked fine. Thanks for all the help and quick replies.