Solved

Building a character string from variable string inputs

Posted on 2004-08-09
8
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 25 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 200
sum67 challenge 35 116
child constructor and parent constructor, overriding and overloading 6 93
x-bar in Google Sheets 2 91
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 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