Solved

Building a character string from variable string inputs

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

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

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

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…
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

622 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