Building a character string from variable string inputs
Posted on 2004-08-09
I am writing a script to pass a string to another function. To simplify the description, I need to build a string with the following sample requirement.
1. String is 100 characters long. Let's call this $OUTPUT.
2. Characters 31-50 comes from another input string which is variable. Let's call this variable string $INPUT. If $INPUT is less than 20 characters long, I want to right-pad it with spaces to make it exactly 20 characters long. If $INPUT is more than 20 characters long, I want to truncate it to 20 characters.
3. Characters 1-20 of $OUTPUT are spaces.
What's a good way of formatting $INPUT as required above, and then putting it as the 31st-to-50th character in the $OUTPUT string? What's the elegant way of setting characters 1-20 as spaces, without resorting to actually typing out 20 spaces in quotes?