Re: How split the input path into components



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
.



Relevant Pages

  • Shell Command Issue in VB6.0
    ... The application which i am calling using shell resides in the ... Why the heck i am getting this error..when it works on some other ... Urgent Reply please. ...
    (microsoft.public.vb.general.discussion)
  • Re: Unable to ftp into UNIX machine
    ... One is using bash and the other is using tcsh. ... > the problem might be related to the shell? ... any email with the word "urgent" in the subject or address will be deleted. ...
    (comp.unix.questions)
  • Re: Sed question
    ... other posters posted this sed command, ... Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ...
    (comp.unix.shell)
  • Re: How does this work?
    ... Bash4 does not do decimal fractions: ... Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ...
    (comp.unix.shell)
  • Re: How does this work?
    ... shell, as the shell itself can do integer arithmetic. ... $ calc 2+2 '3*3' ... Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ...
    (comp.unix.shell)