Solved

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

Posted on 2004-10-04
15
728 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Process filename extension 3 156
sumNumber challenge 16 99
if loop in java 3 114
sum13 challenge 24 77
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

20 Experts available now in Live!

Get 1:1 Help Now