Unix Shell - String extraction

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > String extraction





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 String extraction
hiddenmarkov@yahoo.com

2006-10-19, 7:32 am

hi,

Could anyone help me to extract this string: /a/b/c/xyz to /a/b/c or
/a/b/xyz to /a/b ?

Thanks,
NewB.

Vino

2006-10-19, 7:32 am

hiddenmarkov@yahoo.com wrote:
> hi,
>
> Could anyone help me to extract this string: /a/b/c/xyz to /a/b/c or
> /a/b/xyz to /a/b ?
>
> Thanks,
> NewB.
>

dirname /a/b/c/xyz

Vino.
Janis

2006-10-19, 7:32 am

hiddenmarkov@yahoo.com wrote:
> hi,
>
> Could anyone help me to extract this string: /a/b/c/xyz to /a/b/c or
> /a/b/xyz to /a/b ?


$ s=/a/b/c/xyz ; echo "${s%/*}"
/a/b/c
$ s=/a/b/xyz ; echo "${s%/*}"
/a/b

Janis

>
> Thanks,
> NewB.


hiddenmarkov@yahoo.com

2006-10-19, 1:21 pm


Vino wrote:
> hiddenmarkov@yahoo.com wrote:
> dirname /a/b/c/xyz
>
> Vino.


I mean if the string is "/a/b/c/xyz.txt" or "/a/b/xyz.txt", can I
extract the directory path?

NewB.

Radoulov, Dimitre

2006-10-19, 1:21 pm

> Vino wrote:
>
> I mean if the string is "/a/b/c/xyz.txt" or "/a/b/xyz.txt", can I
> extract the directory path?


So?

$ str="/a/b/c/xyz.txt"
$ dirname "$str"
/a/b/c
$ str="/a/b/xyz.txt"
$ dirname "$str"
/a/b


Regards
Dimitre


Radoulov, Dimitre

2006-10-19, 1:21 pm

>>> > Could anyone help me to extract this string: /a/b/c/xyz to /a/b/c or
>
> So?
>
> $ str="/a/b/c/xyz.txt"
> $ dirname "$str"
> /a/b/c
> $ str="/a/b/xyz.txt"
> $ dirname "$str"
> /a/b


And if you want the filename:

$ str="/a/b/c/xyz.txt"
$ basename "$str"
xyz.txt
$ str="/a/b/xyz.txt"
$ basename "$str"
xyz.txt


Regards
Dimitre


Vino

2006-10-19, 1:21 pm

Janis wrote:
> hiddenmarkov@yahoo.com wrote:
>
>
>
> $ s=/a/b/c/xyz ; echo "${s%/*}"
> /a/b/c
> $ s=/a/b/xyz ; echo "${s%/*}"
> /a/b

I wanted to suggest the shell builtin construct as well. But dirname and
the ${paramter%/*} construct dont have the same behaviour always. This
is what I mean

$ s=/a/b/c/xyz ; echo "${s%/*}"
/a/b/c
$ s=/a/b/c/xyz/ ; echo "${s%/*}"
/a/b/c/xyz
$ dirname /a/b/c/xyz/
/a/b/c
$ dirname /a/b/c/xyz
/a/b/c

See the difference in behaviour when there is trailing path separator.

Vino.
>
> Janis
>
>
>
>

hiddenmarkov@yahoo.com

2006-10-19, 1:21 pm


Radoulov, Dimitre wrote:
>
> And if you want the filename:
>
> $ str="/a/b/c/xyz.txt"
> $ basename "$str"
> xyz.txt
> $ str="/a/b/xyz.txt"
> $ basename "$str"
> xyz.txt
>
>
> Regards
> Dimitre


Thank you for suggestion!

Janis

2006-10-19, 1:21 pm

Vino wrote:
> Janis wrote:
> I wanted to suggest the shell builtin construct as well. But dirname and
> the ${paramter%/*} construct dont have the same behaviour always.


Why should the parameter expansion have "the same behaviour" as
dirname; that is a complete different question.

> This
> is what I mean
>
> $ s=/a/b/c/xyz ; echo "${s%/*}"
> /a/b/c
> $ s=/a/b/c/xyz/ ; echo "${s%/*}"
> /a/b/c/xyz
> $ dirname /a/b/c/xyz/
> /a/b/c
> $ dirname /a/b/c/xyz
> /a/b/c
>
> See the difference in behaviour when there is trailing path separator.


A trailing slash in the data does not match the OP's question.

Anyway, also in the case you have in mind I'd prefer using a second
shell internal instead of an external program[*]...

$ s=/a/b/c/xyz ; s=${s%/}; echo ${s%/*}
/a/b/c
$ s=/a/b/c/xyz/ ; s=${s%/}; echo ${s%/*}
/a/b/c

Or even...

$ s=/a/b/c/xyz ; echo ${s%/*?(/)}
/a/b/c


Janis

[*] BTW, some shells (like the ksh93 I am using) have a builtin
dirname.
[vbcol=seagreen]
> Vino.

Janis

2006-10-19, 1:21 pm

Janis wrote:
> Vino wrote:
>
> Why should the parameter expansion have "the same behaviour" as
> dirname; that is a complete different question.
>
>
> A trailing slash in the data does not match the OP's question.
>
> Anyway, also in the case you have in mind I'd prefer using a second
> shell internal instead of an external program[*]...
>
> $ s=/a/b/c/xyz ; s=${s%/}; echo ${s%/*}
> /a/b/c
> $ s=/a/b/c/xyz/ ; s=${s%/}; echo ${s%/*}
> /a/b/c
>
> Or even...
>
> $ s=/a/b/c/xyz ; echo ${s%/*?(/)}
> /a/b/c


The last one was wrong (why?). In ksh you can do...

$ s=/a/b/c//xyz/// ; echo ${s%%+(/)*([!/])*(/)}
/a/b/c
[vbcol=seagreen]
>
> Janis
>
> [*] BTW, some shells (like the ksh93 I am using) have a builtin
> dirname.
>

Chris F.A. Johnson

2006-10-19, 1:21 pm

On 2006-10-19, Vino wrote:
> Janis wrote:
> I wanted to suggest the shell builtin construct as well. But dirname and
> the ${paramter%/*} construct dont have the same behaviour always. This
> is what I mean
>
> $ s=/a/b/c/xyz ; echo "${s%/*}"
> /a/b/c
> $ s=/a/b/c/xyz/ ; echo "${s%/*}"
> /a/b/c/xyz
> $ dirname /a/b/c/xyz/
> /a/b/c
> $ dirname /a/b/c/xyz
> /a/b/c
>
> See the difference in behaviour when there is trailing path separator.


Of course there's a difference. If you give different input, you'll
get a different result. The parameter expansion returns whatever
precedes the last slash. If you want something different, use
different code.

If you want a full shell replacement for the POSIX specification of
dirname, there is this:
<http://cfaj.freeshell.org/src/scripts/dirname-sh>.

If you want domething different, then code for it. If you want to
remove any trailing slash before taking the last part of the
pathname, just do it:

s=/a/b/c/xyz/
sa=${s%/}
echo ${sa%/*}

--
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
Chris F.A. Johnson

2006-10-19, 1:21 pm

On 2006-10-19, Janis wrote:
> Vino wrote:
> Or even...
>
> $ s=/a/b/c/xyz ; echo ${s%/*?(/)}
> /a/b/c


That is not POSIX, nor even bash.

> [*] BTW, some shells (like the ksh93 I am using) have a builtin
> dirname.


What version do you have? I have Version M 1993-12-28 n, and it
doesn't have dirname built in.

--
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
Janis Papanagnou

2006-10-19, 1:21 pm

Chris F.A. Johnson wrote:
> On 2006-10-19, Janis wrote:
>
>
>
> That is not POSIX, nor even bash.
>
>
>
>
> What version do you have? I have Version M 1993-12-28 n, and it
> doesn't have dirname built in.
>


It was 'q', AFAIR, so at least any newer version might support it.

This is from my current environment (Version M 1993-12-28 r)...

$ whence -a dirname
dirname is a tracked alias for /usr/bin/dirname
$ builtin dirname
$ whence -a dirname
dirname is a shell builtin
dirname is a tracked alias for /usr/bin/dirname
$ type dirname
dirname is a shell builtin
$ dirname /a/b//c//xyz.c///
/a/b//c

And 'dirname --man' provides the AT&T man page.

Janis
Chris F.A. Johnson

2006-10-19, 1:21 pm

On 2006-10-19, Janis Papanagnou wrote:
> Chris F.A. Johnson wrote:
>
> It was 'q', AFAIR, so at least any newer version might support it.
>
> This is from my current environment (Version M 1993-12-28 r)...
>
> $ whence -a dirname
> dirname is a tracked alias for /usr/bin/dirname
> $ builtin dirname


Ah! It has to be enabled...

Yes, my version does have it, then.

> $ whence -a dirname
> dirname is a shell builtin
> dirname is a tracked alias for /usr/bin/dirname
> $ type dirname
> dirname is a shell builtin
> $ dirname /a/b//c//xyz.c///
> /a/b//c
>
> And 'dirname --man' provides the AT&T man page.



--
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
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com