Solved

Building a character string from variable string inputs

Posted on 2004-08-09
8
302 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 20

Accepted Solution

by:
tfewster earned 25 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
 
LVL 48

Assisted Solution

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

OUTPUT=$(printf "%30s%-20s" ' ' $INPUT)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now