Solved

Korn shell function to substitute a string within a larger string based on given offset & length

Posted on 2004-09-01
5
680 Views
Last Modified: 2013-12-26
Korn shell function to substitute a string within a larger string based on given offset & length without using awk, Perl, or anything but pure korn shell. Example: Record 000RI01990305ABC12345678901234567890abcdefghiabcdefghi. I want to pass into the function this record, my new string of 991305XYZ, starting possition of  7 for a length of 9. Any ideas? Any assistance would be greatly appreciated.

0
Comment
Question by:rayskelton
  • 2
  • 2
5 Comments
 
LVL 38

Accepted Solution

by:
yuzh earned 250 total points
ID: 11959731
You can use "cut" to do the job"

eg:

NEWSTR="991305XYZ"
OLD="000RI01990305ABC12345678901234567890abcdefghiabcdefghi."

REC1=`echo $OLD | cut -c1-6`
REC2=`echo $OLD | cut -c15-`

NEWRECORD=${REC1}${NEWSTR}${REC2}

0
 

Author Comment

by:rayskelton
ID: 11959975
Is there any other built in function or parameter? One more note: I am using the old 88 version of korn shell.
0
 
LVL 38

Expert Comment

by:yuzh
ID: 11960043
"cut" will work for any vserion of *nix!

You can also use awk (substr()) function to do the job.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11961801
Sounds very much like a homework question, but I'll give you benefit of the doubt

NEWSTR="991305XYZ"
OLD="000RI01990305ABC12345678901234567890abcdefghiabcdefghi."

echo ${OLD::6}$NEWSTR${OLD:10}

The above works in bash, but I'm pretty certain it should be fine for ksh.
0
 

Author Comment

by:rayskelton
ID: 11962230
The cut I have been using, but was looking more to a function. Perl works good for this, but looking for a simple solution native to many variations of Solaris. I guess piping the string into awk could work but haven't tried yet.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 …
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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