Solved

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

Posted on 2004-09-01
5
692 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 132
repeateFront java challenge 31 94
how to monitor remote shell execution on linux 9 104
Modbus - whats the maximum I can store in one register? 4 80
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: 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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 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