• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 773
  • Last Modified:

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

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
rayskelton
Asked:
rayskelton
  • 6
  • 6
  • 2
  • +1
1 Solution
 
tfewsterCommented:
printf "%-Xs" $yourtext
0
 
TintinCommented:
Depends exactly what you mean by "old version of ksh".

From memory, I think you can do:

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


0
 
rayskeltonAuthor Commented:
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
chris_calabreseCommented:
use 'read -r resLine' instead of 'read resLine'
0
 
rayskeltonAuthor Commented:
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
 
chris_calabreseCommented:
Yeah, use a modern version of ksh. http://www.research.att.com/sw/download/
0
 
rayskeltonAuthor Commented:
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
 
chris_calabreseCommented:
In that case, don't write your program in ksh. How about Perl?
0
 
rayskeltonAuthor Commented:
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
 
chris_calabreseCommented:
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
 
rayskeltonAuthor Commented:
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
 
chris_calabreseCommented:
Hmm, works fine for me. But I don't have any systems with a ksh earlier than ksh88.
0
 
TintinCommented:
What version of Solaris are you running?  They must be *very* old if they don't have ksh88 or better on them.
0
 
rayskeltonAuthor Commented:
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
 
chris_calabreseCommented:
Umm, this is ksh88 (hence the 88), and is the same version I tested my read -r line on.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 6
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now