Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 757
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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