Re: How split the input path into components
- From: "Chris F.A. Johnson" <cfajohnson@xxxxxxxxx>
- Date: Mon, 30 Jul 2007 03:11:22 -0400
On 2007-07-30, ramesh.btech@xxxxxxxxx wrote:
I have a simple problem. but its urgent.
I have a variable in shell. say
$PATH'='/p2k/c9.1/IRB/shell/data";
You shouldn't use a variable named PATH; it conflicts with a
standard variable. Let's assume:
path=/p2k/c9.1/IRB/shell/data
I need to cut this string with dilimeter 'IRB'. So I want the result
as
$str1 ='/p2k/c9.1/IRB/' or '/p2k/c9.1/IRB'
$str2='shell/data' or '/shell/data'
if $str1= '/p2k/c9.1/' also OK for me.
I tried in this way;
echo "p2k/c9.1/IRB/shell/data"|{ IFS="IRB" read a b c; echo $c;}
but I got 'B/shell/data' I don't why 'B' came?
Use parameter expansion:
str2=${path#*/IRB} ## Or: str2=${path#*/IRB/}
str1=${path%"$str2"}
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
.
- References:
- How split the input path into components
- From: ramesh . btech
- How split the input path into components
- Prev by Date: How split the input path into components
- Next by Date: Re: tar problem
- Previous by thread: How split the input path into components
- Index(es):
Relevant Pages
|