PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > How split the input path into components
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

How split the input path into components

Réponse
 
LinkBack Outils de la discussion
Vieux 30/07/2007, 07h56   #1
ramesh.btech@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How split the input path into components

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.

  Réponse avec citation
Vieux 30/07/2007, 08h11   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How split the input path into components

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
  Réponse avec citation
Vieux 30/07/2007, 08h11   #3
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How split the input path into components

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h09.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20606 seconds with 11 queries