Operating System - HP-UX
1754975 Members
3165 Online
108828 Solutions
New Discussion юеВ

Re: substring builtin in ksh

 
Javier Gutierrez
Occasional Contributor

substring builtin in ksh

Hi,

Do you know if there is builtin substring funtion under ksh (Solaris version this time)?
9 REPLIES 9
Andreas Voss
Honored Contributor

Re: substring builtin in ksh

Hi,

i know no substring function in ksh but you can use awk for this ie:

var="fullstring"
sub=$(echo $var|awk '{print substr($0,5,3)}')

will set sub to "str"

Regards
John Palmer
Honored Contributor

Re: substring builtin in ksh

Standard ksh doesn't have a substring feature but you can use the following construct to manipulate strings...

(From man sh-posix)

${parameter#pattern}
${parameter##pattern}
If the shell pattern matches the beginning of the value of parameter, the value of this substitution is the value of the parameter with the matched portion deleted; otherwise, the value of this parameter is substituted. In the former case, the smallest matching pattern is deleted; in the latter case, the largest matching pattern is deleted. These characters # or % should be escaped by a backslash (\) or quotes ('').

${parameter%pattern}
${parameter%%pattern}
If the shell pattern matches the end of the value of parameter, the value of parameter with the matched part is deleted; otherwise, substitute the value of parameter. In the former, the smallest matching pattern is deleted; in the latter, the largest matching pattern is deleted. These characters # or % should be escaped by a backslash (\) or quotes ('').

Regards,
John
Jean-Luc Oudart
Honored Contributor

Re: substring builtin in ksh

I don't know sucha function but you may build your own, such as :
#!/bin/sh

# substr

if [ $# -ne 3 ]; then
exit 2
fi
lg=$(( $2 + $3 - 1 ))
echo $1 | cut -c$2-$lg

~~~~~~~~~~~~~~~~~~
oudartj:/home/oudartj/k1 $ substr abcdef 3 2
cd


Jean-Luc
fiat lux
Javier Gutierrez
Occasional Contributor

Re: substring builtin in ksh

Guys thank you very much.

I know that in linux you can do with variables just:
> a=example
> echo ${a:0:2}
exa

I didn't want to use awk and cut, just to optimize and do it faster, but I can see there is no other way.

Thank you so much.

Christophe MAILHE
Frequent Advisor

Re: substring builtin in ksh

Hi Javier,

This functionality appears in ksh version newer than 11/16/88.

Unfortunately, the standard ksh version in HP-UX is 11/16/88 and so does include the latest ksh enhancement.

However sh-posix include a few , but not all of these.
James R. Ferguson
Acclaimed Contributor

Re: substring builtin in ksh

Hi:

While certainly not as conservative as a shell built-in you could use:

# expr substr expr1 expr2 expr3

This takes the substring expr1, starting at expr2 for the length given by expr3.

See the man pages for 'expr'.

Regards!

...JRF...
harry d brown jr
Honored Contributor

Re: substring builtin in ksh

Javier,

You said:

**********************************
I didn't want to use awk and cut, just to optimize and do it faster, but I can see there is no other way.
**********************************

Are you kidding me??

awk is a thousand (maybe a little exaggeration) times faster than ksh could ever imagine being.

And if you are serious about doing any kind of string parsing you should use either awk or better yet: perl!!!


live free or die
harry
Live Free or Die
monica dastous
New Member

Re: substring builtin in ksh

Hi,
I'm trying to do a similar thing. I have the string:
" N SAS Shared Components" Notice the spaces. I need to substring from 0-14 15-26 27-48. I tried the command above, but it starts with the first non-blank character (N). Any suggestions?
Thanks,
MOnica
curt larson_1
Honored Contributor

Re: substring builtin in ksh

as mr mailhe points out /usr/bin/ksh in hp-ux is the 88 version, which doesn't include the majority of enhancements that are in the 93 version.

but, quite a few of the 93 enhancements are included in /usr/dt/bin/dtksh. using this shell you will be able to do a substring using an offset and length.

monica, your probably better off using the cut command.