Solved

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

Posted on 2004-09-01
5
713 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

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