|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi All!
I have a simple problem. but its urgent. I have a variable in shell. say $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? Please me ASSAP. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 2007-07-30, ramesh.btech@gmail.com 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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On 2007-07-30, ramesh.btech@gmail.com 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 |
|
![]() |
| Outils de la discussion | |
|
|