Computing.Net > Forums > Unix > How to pad spaces

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to pad spaces

Reply to Message Icon

Name: Sarah
Date: September 26, 2002 at 01:45:51 Pacific
OS: AIX 4.3
CPU/Ram: Don't know
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: jimbo
Date: September 26, 2002 at 03:07:15 Pacific
Reply:

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


0

Response Number 2
Name: ASB
Date: September 26, 2002 at 08:59:38 Pacific
Reply:

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


0

Response Number 3
Name: Jerry Lemieux
Date: September 26, 2002 at 17:38:27 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Track user directories ftp



Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: How to pad spaces

How to remove spaces in filenames www.computing.net/answers/unix/how-to-remove-spaces-in-filenames/8204.html

how to find amount of free disk space www.computing.net/answers/unix/how-to-find-amount-of-free-disk-space-/3509.html

How to find CPU usage, RAM, Hard Disk www.computing.net/answers/unix/how-to-find-cpu-usage-ram-hard-disk/3417.html