|
Home > Archive > Unix Shell > May 2007 > Extracting path components from an absolute path
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Extracting path components from an absolute path
|
|
| fia_wrc_fanatic 2007-05-29, 1:22 pm |
| 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 help is appreciated.
Thanks.
| |
| Walt Fles 2007-05-29, 1:22 pm |
| 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 help is appreciated.
>
> Thanks.
echo $INSTALLROOT | cut -f2 -d"/"
echo $INSTALLROOT | cut -f3- -d"/"
It could be done better with ksh.
| |
| Bill Marcum 2007-05-29, 1:22 pm |
| 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 "/","");print}'
> 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.
| |
| Lew Pitcher 2007-05-29, 7:21 pm |
| 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#/*/}
| |
| Chris F.A. Johnson 2007-05-29, 7:21 pm |
| 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
|
|
|
|
|