Operating System - HP-UX
1833875 Members
3102 Online
110063 Solutions
New Discussion

Possible ksh ${parameter##pattern} substring?

 
SOLVED
Go to solution
A. Daniel King_1
Super Advisor

Possible ksh ${parameter##pattern} substring?

Is there a way to combine

${parameter##pattern}

and

${parameter%%pattern}

to pull out a substring? As in something like:

${parameter##pattern%%pattern}

or

${parameter##pattern:parameter%%pattern}

I've seen the forum item:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x69246049dbb6d611abdb0090277a778c,00.html

And indeed,

$a=example
$echo ${a:5:6}
le

Does seem to do as expected. Any thoughts, gurus?
Command-Line Junkie
7 REPLIES 7
A. Daniel King_1
Super Advisor

Re: Possible ksh ${parameter##pattern} substring?

Sorry!

I just realized that for the last:

$echo ${a:5:6}

I was in bash! The question still applies, though - and extra points for those who can tell me how to do this in _bash_ as well!
Command-Line Junkie
John Poff
Honored Contributor
Solution

Re: Possible ksh ${parameter##pattern} substring?

Hi,

I played with the pattern substitutions in both ksh and bash, and I don't think they will work as you show them. They can't be combined on a single line, but you can split them into two lines. Something like this will work:

MYSTRING=mystring.txt
echo "MYSTRING=$MYSTRING"
MYSTRING=${MYSTRING##my}
MYSTRING=${MYSTRING{%%.txt}
echo "MYSTRING=$MYSTRING"

will return

MYSTRING=mystring.txt
MYSTRING=string

You could probably write a function where you could pass the parameter (variable) and the patterns to it, and have it return the result to you. The easier way would be to use 'awk' as shown in the thread you listed.

This is one of those examples where you are trying to do something that is hard to do in ksh or bash, but is probably quite easy to do in Perl.

JP

Vincent Stedema
Esteemed Contributor

Re: Possible ksh ${parameter##pattern} substring?

Hi,

${${parameter##pattern}%%pattern}

Don't know if it works, but you could give it a try...

Regards,

Vincent
John Poff
Honored Contributor

Re: Possible ksh ${parameter##pattern} substring?

Hi Vincent,

I had tried your suggestion and ksh didn't like it:

#!/usr/bin/ksh

# pattern.ksh

MYSTRING=mystring.txt
echo "MYSTRING=$MYSTRING"

MYSTRING=${MYSTRING##my}
STRING1=${${MYSTRING##my}%%.txt}
echo "STRING1=$STRING1"


Which gives this result:

>./pattern.ksh
MYSTRING=mystring.txt
./pattern.ksh[9]: STRING1=${${MYSTRING##my}%%.txt}: bad substitution


I tried it with some quote marks and it didn't like those either:

STRING1=${"${MYSTRING##my}"%%.txt}

./pattern.ksh[9]: STRING1=${"${MYSTRING##my}"%%.txt}: bad substitution



JP
Vincent Stedema
Esteemed Contributor

Re: Possible ksh ${parameter##pattern} substring?

John: Yeah, I know. Bash doesn't like it either... The problem is with the substitution: it returns the value of the substitution, instead of a variable with a substituted value.

Daniel:

Here's a multi-line solution which doesn't use temp variables, except for the patterns...

p1="pattern1"
p2="pattern2"
${parameter:${#p1}:$(( ${#parameter} - ${#p1} - ${#p2} ))}

Proof that it works:

vincent@magnolia ~
$ a="shellprogrammingisneat"

vincent@magnolia ~
$ p1="shell"

vincent@magnolia ~
$ p2="neat"

vincent@magnolia ~
$ echo ${a:${#p1}:$(( ${#a} - ${#p1} - ${#p2}))}
programmingis

Don't know if this what you're looking for, though.

Regards,

Vincent
John Wright_1
Advisor

Re: Possible ksh ${parameter##pattern} substring?

Hi,

Here's a 1 liner that works for ksh :-)

$ a=shellprogrammingisneat
$ b=${a##shell} echo ${b%%neat}
programmingis
$

Cheers,
JW.
A. Daniel King_1
Super Advisor

Re: Possible ksh ${parameter##pattern} substring?

I've actually done some timings on shell-level extraction versus awk extractions.

awk seems to be faster in most cases.

I was interested, though, in pushing the limits of the shell, and I appreciate the thoughts you have expressed.

I was a little surprised, though. Somewhere I had read that, as a rule, calling external programs slows down a script, and that shell internals should be, generally speaking, faster.

Live and learn, I suppose.
Command-Line Junkie