Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Building a character string from variable string inputs

Posted on 2004-08-09
8
Medium Priority
?
311 Views
Last Modified: 2013-12-26
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?

Thanks.
0
Comment
Question by:mych
  • 4
  • 3
8 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 11757409
If characters 1-20 of $OUTPUT are spaces and characters 31-50 come from $INPUT, then what are characters 21-30 consist of?

There's two ways of getting INPUT the way you need.

First, if you use ksh, you can do

typeset -L20 INPUT

a more generic way is

INPUT=$(printf "%-20s" $INPUT)
0
 
LVL 21

Accepted Solution

by:
tfewster earned 100 total points
ID: 11757492
Adding another elemtn to Tintins analysis:
TRUNC=`printf "%.20s" $INPUT`
printf "%-20s" $TRUNC
0
 

Author Comment

by:mych
ID: 11759700
Ooops, my mistake.. Should be characters 1-30 of $OUTPUT are spaces. But I guess that shouldn't change the solution much. I am using POSIX shell.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 100 total points
ID: 11767811
In that case, just do

OUTPUT=$(printf "%30s%-20s" ' ' $INPUT)
0
 

Author Comment

by:mych
ID: 11780524
Thanks, I tried out the following (combination of Tintin's and tfewsters's suggestions) and it works!
==================================
INPUT=1234567890
echo "old value of INPUT is $INPUT"
TRUNC=`printf "%.20s" $INPUT`
OUTPUT=$(printf "%30s%-20s" ' ' $TRUNC)
echo "OUTPUT is [${OUTPUT}]"

$ test.sh
old value of INPUT is 1234567890
OUTPUT is [                              1234567890          ]
===================================

=====================================
INPUT=12345678901234567890ABCDEFG
echo "old value of INPUT is $INPUT"
TRUNC=`printf "%.20s" $INPUT`
OUTPUT=$(printf "%30s%-20s" ' ' $TRUNC)
echo "OUTPUT is [${OUTPUT}]"

$ test.sh
old value of INPUT is 12345678901234567890ABCDEFG
OUTPUT is [                              12345678901234567890]
======================================
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11780562
Or you could have just done:

INPUT=1234567890
echo "old value of INPUT is $INPUT"
OUTPUT=$(printf "%30s%-20s" ' ' $INPUT)
echo "OUTPUT is [$OUTPUT]"

or even

INPUT=1234567890
echo "old value of INPUT is $INPUT"
printf "%30s%-20s" ' ' $INPUT


0
 

Author Comment

by:mych
ID: 11780812
Hi Tintin,

I have tried doing that. It pads the INPUT string to 20 characters if it was originally less than 20 characters, but it does not truncate the INPUT string to 20 characters if the string was originally more than 20 characters.

============================================
INPUT=12345678901234567890ABCEDFG
echo "old value of INPUT is $INPUT"
OUTPUT=$(printf "%30s%-20s" ' ' $INPUT)
echo "OUTPUT is [$OUTPUT]"

$ test1.sh
old value of INPUT is 12345678901234567890ABCEDFG
OUTPUT is [                              12345678901234567890ABCEDFG]
============================================

============================================
INPUT=12345678901234567890ABCEDFG
echo "old value of INPUT is $INPUT"
OUTPUT=$(printf "%30s%-20s" ' ' $INPUT)
echo "OUTPUT is [$OUTPUT]"

$ test2.sh
old value of INPUT is 12345678901234567890ABCEDFG
OUTPUT is [                              12345678901234567890ABCEDFG]
============================================
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11789021
My mistake.  I didn't test all cases.

%-20s  will pad a string but not trunctate
%.20s  will truncate but not pad.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question