Solved

Building a character string from variable string inputs

Posted on 2004-08-09
8
304 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 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