niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
replacing characters in ksh88 variables
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
Author Message
Dan Peduzzi
*nix forums beginner


Joined: 07 Feb 2005
Posts: 2

PostPosted: Wed Feb 09, 2005 1:01 pm    Post subject: Re: replacing characters in ksh88 variables Reply with quote

Janis Papanagnou wrote:
Quote:
Icarus Sparry wrote:

With ksh93 there are the ${var:offset} substitutions as well.

Yes, but since he explicitely stated ksh88 it's likely that he has
neither
a ksh93 available in his environment nor the possibility to install
one.


That's exactly the case. At this point, I can only look at ksh93
enhancements with extreme jealousy. :-)

The typeset -L technique was exactly what I needed in ksh88. Thank
you...everything is working perfectly.

Dan
Back to top
Janis Papanagnou
*nix forums Guru


Joined: 24 Feb 2005
Posts: 450

PostPosted: Mon Feb 07, 2005 10:15 pm    Post subject: Re: replacing characters in ksh88 variables Reply with quote

Icarus Sparry wrote:
Quote:
Dan Peduzzi wrote:

Is there a way to do this natively within ksh88, or should I look to a
UNIX utility for this sort of thing?

With ksh93 there are the ${var:offset} substitutions as well.

Yes, but since he explicitely stated ksh88 it's likely that he has neither
a ksh93 available in his environment nor the possibility to install one.

Janis
Back to top
Icarus Sparry
*nix forums Guru


Joined: 19 Feb 2005
Posts: 342

PostPosted: Mon Feb 07, 2005 9:35 pm    Post subject: Re: replacing characters in ksh88 variables Reply with quote

On 2005-02-07, Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
Quote:
Dan Peduzzi wrote:
For variables in the Korn shell, is it possible to replace characters
based upon their position only? For example, let's say I have

var="abcdefghi"

and I want to change the substring starting at position 4 and lasting 3
characters (to "123", in this case):

var="abc123ghi"

Is there a way to do this natively within ksh88, or should I look to a
UNIX utility for this sort of thing?

I have no ksh88 handy but think that this quick shot should outline
you at least one possibile way...

typeset var="abcdefghi"
typeset -i pos=4
typeset -i len=3
typeset subst="123"

typeset -L$((pos-1)) left1="$var"
typeset -L$((pos+len-1)) left2="$var"

print - "${left1}${subst}${var#$left2}"

With ksh93 there are the ${var:offset} substitutions as well.

var="abcdefghi"
newvar=${var:0:$((4-1))}"123"${var:$((4-1+3))}

The counts are from 0, hence the '-1' in the calculations.
Also works in bash 3.0
Back to top
Janis Papanagnou
*nix forums Guru


Joined: 24 Feb 2005
Posts: 450

PostPosted: Mon Feb 07, 2005 8:24 pm    Post subject: Re: replacing characters in ksh88 variables Reply with quote

Dan Peduzzi wrote:
Quote:
For variables in the Korn shell, is it possible to replace characters
based upon their position only? For example, let's say I have

var="abcdefghi"

and I want to change the substring starting at position 4 and lasting 3
characters (to "123", in this case):

var="abc123ghi"

Is there a way to do this natively within ksh88, or should I look to a
UNIX utility for this sort of thing?

I have no ksh88 handy but think that this quick shot should outline
you at least one possibile way...

typeset var="abcdefghi"
typeset -i pos=4
typeset -i len=3
typeset subst="123"

typeset -L$((pos-1)) left1="$var"
typeset -L$((pos+len-1)) left2="$var"

print - "${left1}${subst}${var#$left2}"


Janis
Back to top
Dan Peduzzi
*nix forums beginner


Joined: 07 Feb 2005
Posts: 2

PostPosted: Mon Feb 07, 2005 3:47 pm    Post subject: replacing characters in ksh88 variables Reply with quote

For variables in the Korn shell, is it possible to replace characters
based upon their position only? For example, let's say I have

var="abcdefghi"

and I want to change the substring starting at position 4 and lasting 3
characters (to "123", in this case):

var="abc123ghi"

Is there a way to do this natively within ksh88, or should I look to a
UNIX utility for this sort of thing?
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
The time now is Fri Jan 09, 2009 12:52 am | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts IMAP4 search with special characters Luis Corrales python 0 Fri Jul 21, 2006 7:51 am
No new posts Quoting, variables, and xterm graftonfot@yahoo.com shell 2 Thu Jul 20, 2006 2:26 pm
No new posts Question on comparing to variables containing integers huub Perl 14 Thu Jul 20, 2006 11:02 am
No new posts Interpret variables in strings in ksh greenhakan@gmail.com shell 2 Wed Jul 19, 2006 2:03 pm
No new posts Retrieve ext. variables in python program alfa1234 python 7 Wed Jul 19, 2006 7:59 am

CreditCards | Actress | Credit Cards | Web Hosting by Safehosting | eHarmony Coupon
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1563s ][ Queries: 20 (0.0716s) ][ GZIP on - Debug on ]