Re: [Help-bash] How to extract matching suffix pattern in bash?
From:
Chet Ramey
Subject:
Re: [Help-bash] How to extract matching suffix pattern in bash?
Date:
Wed, 13 Mar 2013 22:04:39 -0400
User-agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130216 Thunderbird/17.0.3
On 3/13/13 6:21 PM, Peng Yu wrote:
> Hi,
>
> ${parameter%word} remove matching suffix pattern. I need something
> extract only the matching suffix which is to be not available
> according to bash manual. Do I miss anything?
This is totally untested, but should be fairly self-explanatory, and is
portable to any Posix shell:
suffix_pattern=whatever
prefix=${string%$suffix_pattern}
suffix=${string##$prefix}
The idea is that you chop off the portion of the string matching the suffix
pattern, leaving the prefix, then remove the prefix.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU address@hidden http://cnswww.cns.cwru.edu/~chet/