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 > Extracting path components from an absolute path
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Extracting path components from an absolute path

Réponse
 
LinkBack Outils de la discussion
Vieux 29/05/2007, 17h45   #1
fia_wrc_fanatic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Extracting path components from an absolute path

I am trying to write a Bash script that extracts some or all of the
path components from an absolute path.
For example, if I have the following absolute path:

INSTALLROOT=/opt/vendor/product/

I need to be able to extract the first path component ("opt" from the
example above), as well as the remaining string ("vendor/product" from
the example above).

What's the best way to do this from a Bash script? I have available
awk, sed, and perl but am not familiar with them to be able to
accomplish this! In my use cases, the absolute path will never have
more than 5 or 6 components (i.e. it will not be any deeper than 5 or
6 sub-directory levels).

Any is appreciated.

Thanks.

  Réponse avec citation
Vieux 29/05/2007, 18h06   #2
Walt Fles
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Extracting path components from an absolute path

On May 29, 11:45 am, fia_wrc_fanatic <fia_wrc_fana...@yahoo.com>
wrote:
> I am trying to write a Bash script that extracts some or all of the
> path components from an absolute path.
> For example, if I have the following absolute path:
>
> INSTALLROOT=/opt/vendor/product/
>
> I need to be able to extract the first path component ("opt" from the
> example above), as well as the remaining string ("vendor/product" from
> the example above).
>
> What's the best way to do this from a Bash script? I have available
> awk, sed, and perl but am not familiar with them to be able to
> accomplish this! In my use cases, the absolute path will never have
> more than 5 or 6 components (i.e. it will not be any deeper than 5 or
> 6 sub-directory levels).
>
> Any is appreciated.
>
> Thanks.


echo $INSTALLROOT | cut -f2 -d"/"
echo $INSTALLROOT | cut -f3- -d"/"

It could be done better with ksh.

  Réponse avec citation
Vieux 29/05/2007, 18h34   #3
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Extracting path components from an absolute path

On 29 May 2007 09:45:22 -0700, fia_wrc_fanatic
<fia_wrc_fanatic@yahoo.com> wrote:
>
>
> I am trying to write a Bash script that extracts some or all of the
> path components from an absolute path.
> For example, if I have the following absolute path:
>
> INSTALLROOT=/opt/vendor/product/
>
> I need to be able to extract the first path component ("opt" from the
> example above), as well as the remaining string ("vendor/product" from
> the example above).
>
> What's the best way to do this from a Bash script? I have available
> awk, sed, and perl but am not familiar with them to be able to
> accomplish this!


echo "$INSTALLROOT" | awk -F/ '{print $2}'
echo "$INSTALLROOT" | awk -F/ '{sub("/" $2 "/","")rint}'

> In my use cases, the absolute path will never have
> more than 5 or 6 components (i.e. it will not be any deeper than 5 or
> 6 sub-directory levels).
>



--
Whom the gods wish to destroy they first call promising.
  Réponse avec citation
Vieux 29/05/2007, 20h23   #4
Lew Pitcher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Extracting path components from an absolute path

On May 29, 12:45 pm, fia_wrc_fanatic <fia_wrc_fana...@yahoo.com>
wrote:
> I am trying to write a Bash script that extracts some or all of the
> path components from an absolute path.
> For example, if I have the following absolute path:
>
> INSTALLROOT=/opt/vendor/product/
>
> I need to be able to extract the first path component ("opt" from the
> example above), as well as the remaining string ("vendor/product" from
> the example above).
>
> What's the best way to do this from a Bash script?


Would this be what you are looking for?

echo ${INSTALLROOT%/*/*} ${INSTALLROOT#/*/}


  Réponse avec citation
Vieux 29/05/2007, 20h52   #5
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Extracting path components from an absolute path

On 2007-05-29, fia_wrc_fanatic wrote:
> I am trying to write a Bash script that extracts some or all of the
> path components from an absolute path.
> For example, if I have the following absolute path:
>
> INSTALLROOT=/opt/vendor/product/
>
> I need to be able to extract the first path component ("opt" from the
> example above), as well as the remaining string ("vendor/product" from
> the example above).
>
> What's the best way to do this from a Bash script? I have available
> awk, sed, and perl but am not familiar with them to be able to
> accomplish this! In my use cases, the absolute path will never have
> more than 5 or 6 components (i.e. it will not be any deeper than 5 or
> 6 sub-directory levels).


In any POSIX shell (not just bash):

IFS=/
set -f
set -- $INSTALLROOT

The components are now distributed in the positional parameters,
$1 (opt), $2 (vendor), $3 (product) ...

If you just need the last field:

last=${INSTALLROOT##*/}


--
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 13h22.


É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,10349 seconds with 13 queries