get part of string using shell script

get part of string using shell script

am 07.05.2007 10:05:08 von moonhk

Hi Reader
How to get part of substring using shell scirpt ?

e.g. qwert.prg return string after "." , result "prg"?

moon_ils

Re: get part of string using shell script

am 07.05.2007 10:56:28 von PDreyer

On May 7, 10:05 am, moonhk wrote:
> Hi Reader
> How to get part of substring using shell scirpt ?
>
> e.g. qwert.prg return string after "." , result "prg"?
>
> moon_ils

fn="qwert.prg"
echo ${fn##*.}

Re: get part of string using shell script

am 08.05.2007 14:19:39 von Guru

On May 7, 1:05 pm, moonhk wrote:
> Hi Reader
> How to get part of substring using shell scirpt ?
>
> e.g. qwert.prg return string after "." , result "prg"?
>
> moon_ils

You can use cut command with . as delimiter.
echo qwert.prg | cut -d'.' -f2
will return the string after .

Re: get part of string using shell script

am 08.05.2007 14:41:29 von Ed Morton

Guru wrote:
> On May 7, 1:05 pm, moonhk wrote:
>
>>Hi Reader
>>How to get part of substring using shell scirpt ?
>>
>>e.g. qwert.prg return string after "." , result "prg"?
>>
>>moon_ils
>
>
> You can use cut command with . as delimiter.
> echo qwert.prg | cut -d'.' -f2
> will return the string after .
>

Yes, but

echo qwert.bob.prg | cut -d'.' -f2

will return "bob" which probably isn't what's wanted. See the other post
in this thread for the right way to do it.

Ed.

Re: get part of string using shell script

am 08.05.2007 19:58:50 von Stephane CHAZELAS

2007-05-08, 07:41(-05), Ed Morton:
[...]
>> You can use cut command with . as delimiter.
>> echo qwert.prg | cut -d'.' -f2
>> will return the string after .
>>
>
> Yes, but
>
> echo qwert.bob.prg | cut -d'.' -f2
>
> will return "bob" which probably isn't what's wanted. See the other post
> in this thread for the right way to do it.
[...]

Also note that cut doesn't return a specific field in a string,
it returns the field in every line of the string. Also, echo
can't be used with arbitrary data (data that is unknown
beforehand such as user provided data).

With a Unix/POSIX sh:

case $var in
(*.*) ext=${var##*.};;
(*) ext=NO-EXTENSION;;
esac

With a Bourne sh (will also work with other shs).

ext=`expr "x$var" : '.*\.\(.*\)'`

(which additionaly returns a non-zero exit status if there's no
extension (unfortunately, it also returns a non-zero status for
extensions like 0 or 00 or -0 or empty extensions...))

You may also want to treat dot files specially (~/.zshrc would
generally not be said to have a "zshrc" extension).

case $var in
(?*.*) ext=${var##*.};;
(*) ext=NO-EXTENSION;;
esac

--
Stéphane

Re: get part of string using shell script

am 08.05.2007 20:22:42 von Stephane CHAZELAS

2007-05-08, 17:58(+00), Stephane CHAZELAS:
[...]
> You may also want to treat dot files specially (~/.zshrc would
> generally not be said to have a "zshrc" extension).
>
> case $var in
> (?*.*) ext=${var##*.};;
> (*) ext=NO-EXTENSION;;
> esac

And as we're at knitpicking, if $var contains a file path rather
than a filename, we'd have to consider cases like
/etc/init.d/foo (the extension is not ".d/foo") or /etc/init.d/
(the extension being "d")

So:

ext=`expr "x$var" : '.*[^/]\.\([^/]*\)/*$'`

A solution with the ${..#..} POSIX operators would be a bit
clumsy.

--
Stéphane