Putting white Space at the end of the string

Hi Guys,
Hope, you all are doing good out there.
I am writing a shell script and currrint in need of your help.

This is what I need to do;
I have position based plain file. One of the fields is 15 character long. I need to fill that field. The problem is that the value is dynamic, it could be 4, 8, 10, 13 or 15 characters long. It is quite possible that most of the time the value is not 15 character long.

If the value is less than 15, then I need to add the no of exact white spaces to make it up to 15.

For examle, the string is " ERI-STD-W800I ". Here, the length of the string is 13, so I need to add 2 white spaces at the end of it. So, the string will be " ERI-STD-W800I ".

Can you please help me in achieving this?

A prompt help will be highly appreciated.

Regards,
Chandan Singh

Assuming you have man printf (linux):

printf "%-15s" $value
1 Like

Although this works fine for the given example, it would be safer to use:

printf "%-15s" "$value"

in case $value sometimes expands to a string containing whitespace characters.

2 Likes

Thanks Don for your help....
Much appreciated.... :b:

An example with awk, field separator : and field #1 and input file /etc/group

awk 'BEGIN {FS=OFS=":"} {$field=sprintf("%-*s",size,$field); print}' field=1 size=15 /etc/group

Here the * in the format string requires the size as an additional argument.
Of course could be directly hacked into the format string like this

awk 'BEGIN {FS=OFS=":"} {$field=sprintf("%-"size"s",$field); print}' field=1 size=15 /etc/group