- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Possible ksh ${parameter##pattern} substring?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2002 02:47 PM
12-20-2002 02:47 PM
${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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2002 02:50 PM
12-20-2002 02:50 PM
Re: Possible ksh ${parameter##pattern} substring?
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2002 08:56 PM
12-20-2002 08:56 PM
SolutionI 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2002 09:21 AM
12-21-2002 09:21 AM
Re: Possible ksh ${parameter##pattern} substring?
${${parameter##pattern}%%pattern}
Don't know if it works, but you could give it a try...
Regards,
Vincent
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2002 12:03 PM
12-21-2002 12:03 PM
Re: Possible ksh ${parameter##pattern} substring?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2002 04:32 AM
12-22-2002 04:32 AM
Re: Possible ksh ${parameter##pattern} substring?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2002 05:27 PM
12-22-2002 05:27 PM
Re: Possible ksh ${parameter##pattern} substring?
Here's a 1 liner that works for ksh :-)
$ a=shellprogrammingisneat
$ b=${a##shell} echo ${b%%neat}
programmingis
$
Cheers,
JW.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2002 03:30 PM
12-30-2002 03:30 PM
Re: Possible ksh ${parameter##pattern} substring?
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.