Unix Shell - replacing characters in ksh88 variables

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2005 > replacing characters in ksh88 variables





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author replacing characters in ksh88 variables
Dan Peduzzi

2005-02-07, 8:47 pm

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?

Janis Papanagnou

2005-02-07, 8:47 pm

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}"


Janis
Janis Papanagnou

2005-02-07, 8:47 pm

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.

Janis
Dan Peduzzi

2005-02-09, 5:56 pm


Janis Papanagnou wrote:
> Icarus Sparry wrote:
>
> 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

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com