Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
I have to write a function to input a Label and a number, and output a line as the following format:
Column 1 to 30: field label, left justified.
Column 31 to 45: A number, right justified.The middle is padded with space. May I know how can I achieve this? (I don't know how to count the length of the label/number, and how to pad the spaces in between.)
Please help.
Many thanks.

I haven't the slightest idea what you need.
Can you give more details please?
Not sure what "the middle padded with space" is saying.-jim

In Solaris:
[589:tragicomix:/export/home/sc02048]> more file
aaaaa 1111
bbb 2222
ccccccc 33
ddddddddddddddddddddddddddddd 123456789012345
[590:tragicomix:/export/home/sc02048]> awk '{printf ("%-30s %15s \n"),$1,$2}' file
aaaaa 1111
bbb 2222
ccccccc 33
ddddddddddddddddddddddddddddd 123456789012345

typeset -L30 variableName
This will left justify a variable and limit the size to 30 characters. It will fill with trailing blanks or truncate the data if it is larger then 30 characters.typeset -R15 variableName
This will right justify a variable and limit the size to 15 characters. It will fill with leading blanks or truncate the data if it is larger than 15 characters.
To find the length of a variable:
echo "${#variableName}"
or
print "${#variableName}"To pad a variable with space is simply a matter of:
echo "${variableName} ${variableName}"
Put as may spaces between the variableName as you want.
The print and typeset commands are Korn shell commands.
Jerry

![]() |
Track user directories
|
ftp
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |