Solved

ksh - How do I right pad a string in the old version of ksh

Posted on 2004-10-04
15
732 Views
Last Modified: 2013-12-26
ksh - How do I right pad a string in the old version of ksh. I must have a fixed record length of X number of bytes. I am inserting and editing this record, but must end up padded to X number of bytes.
0
Comment
Question by:rayskelton
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 20

Expert Comment

by:tfewster
ID: 12221837
printf "%-Xs" $yourtext
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12223988
Depends exactly what you mean by "old version of ksh".

From memory, I think you can do:

typeset -R20 string='abcde'
echo "$string"


0
 

Author Comment

by:rayskelton
ID: 12226639
The printf works good, but I think my problem is a litter more than I first thought. I think my problem stems from a read and I need to convert the read to something else. Below is a snippet of what I am referencing.


This is my current read of a file which I test conditions and edit records. None of my trailing spaces are read from this line.
 cat $AIXX_MVR_RESULT_FILE | while IFS="\n" read resLine

Below is a line, which reads without trimming the trailing white spaces.
     resLine=$(echo "$line" | cut -c0-$editOffset)

How do I incorporate the above line in a while loop as the cat is doing above.



0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 12227015
use 'read -r resLine' instead of 'read resLine'
0
 

Author Comment

by:rayskelton
ID: 12227181
The -r didn't work for me. The -d option is what I need, but am running the pre 88 version of ksh. Any other ideas? The snippet below gives a better picture of what I am doing.



###########################################################
# Replace the result file posn/bsn with the previouly
# captured posn/bsn for AIXX Result
#########################################################
            cat $AIXX_MVR_RESULT_FILE | while read resLine
              do
               aixxRecType=$(echo $resLine | cut -c0-2)
               if [ $aixxRecType == "04" ]; then
                 print "04 Record type found:"$aixxRecType
                 # Substitute posn/bsn
                 typeset substitutionLength=29
            
                 returnedSubString=" "
             print "RESLINE befory function:"$resLine
                 SubStringFunc1 $resLine $QUOTEBACK 5 $substitutionLength
                fi
             resLine=$(printf "%-80s" $resLine)
             print $resLine >>$RESULT_HOME/$UNQ_AIDS_RESULT.aidsMvr
             resLine=" "
              done
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 12227281
Yeah, use a modern version of ksh. http://www.research.att.com/sw/download/
0
 

Author Comment

by:rayskelton
ID: 12227360
I can't use a newer version. I am locked in a large corporate environment with many Sun boxes standardized to the old version. I must use what the sysadmins have standardized the company around.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:chris_calabrese
ID: 12227411
In that case, don't write your program in ksh. How about Perl?
0
 

Author Comment

by:rayskelton
ID: 12227470
Perl would make it easier, but cannot use Perl. I cannot change the scripting language or version to solve this problem. I must find a simple solution to prevent a file read from stripping the white spaces and this problem cannot be that difficult.
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 12227755
You might think the problem is not that difficult, but there's a reason that Dave Korn decided to the semantics of read 16 years ago.

How about something like this:
  cat $AIXX_MVR_RESULT_FILE | sed 's/$/|/' | while IFS="|" read resLine junk
0
 

Author Comment

by:rayskelton
ID: 12228884
I can't get " cat $AIXX_MVR_RESULT_FILE | sed 's/$/|/' | while IFS="|" read resLine junk" to work or any variation. I also tried replacing the sed with cut and no difference.
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 12229192
Hmm, works fine for me. But I don't have any systems with a ksh earlier than ksh88.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12230520
What version of Solaris are you running?  They must be *very* old if they don't have ksh88 or better on them.
0
 

Author Comment

by:rayskelton
ID: 12231219
The version is 11/16/88 and is the same on around 25 Sun boxes. It's a no issue trying to upgrade versions on these machines affecting potentially hundreds of developers and hundreds of mission critical applications. Not worth the breath.
0
 
LVL 14

Accepted Solution

by:
chris_calabrese earned 500 total points
ID: 12237222
Umm, this is ksh88 (hence the 88), and is the same version I tested my read -r line on.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Separate into files by filename 12 76
lucky13 challenge 11 124
MaxSpan challenge 9 82
Problem to event 3 78
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 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

11 Experts available now in Live!

Get 1:1 Help Now