Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

htaccess multiple authuserfile, www.xxx.con, www.xxxcon, wwwxxx100www, www...xxxcon, www.xxxcon, error 1025 mysql errno:150, xxx.con, w.w.w.xxxcon, www,xxx.con

Links

XODOX
Impressum

#1: get part of string using shell script

Posted on 2007-05-07 10:05:08 by moonhk

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

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

moon_ils

Report this message

#2: Re: get part of string using shell script

Posted on 2007-05-07 10:56:28 by PDreyer

On May 7, 10:05 am, moonhk <moon_ils...@yahoo.com.hk> 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##*.}

Report this message

#3: Re: get part of string using shell script

Posted on 2007-05-08 14:19:39 by Guru

On May 7, 1:05 pm, moonhk <moon_ils...@yahoo.com.hk> 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 .

Report this message

#4: Re: get part of string using shell script

Posted on 2007-05-08 14:41:29 by Ed Morton

Guru wrote:
> On May 7, 1:05 pm, moonhk <moon_ils...@yahoo.com.hk> 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.

Report this message

#5: Re: get part of string using shell script

Posted on 2007-05-08 19:58:50 by 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

Report this message

#6: Re: get part of string using shell script

Posted on 2007-05-08 20:22:42 by 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

Report this message