Unix Shell - substring from string

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > April 2004 > substring from string





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 substring from string
Petterson Mikael

2004-04-26, 9:34 am

Hi,

I have the following string,
'/app/eclipse/2.1.3/eclipse' and need the following
substring 'eclipse/2.1.3'

Any hints on how I can do this?

BR

//Mikael
Charles Demas

2004-04-26, 10:34 am

In article <408D102C.48FC985F@era.ericsson.se>,
Petterson Mikael <mikael.petterson@nospam.se> wrote:
>Hi,
>
>I have the following string,
>'/app/eclipse/2.1.3/eclipse' and need the following
>substring 'eclipse/2.1.3'
>
>Any hints on how I can do this?


dirname $string | sed 's:/[a-z]*/:: ; s:.*:'&':'

untested


Chuck Demas

--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
William Park

2004-04-26, 1:34 pm

Petterson Mikael <mikael.petterson@era.ericsson.se> wrote:
> Hi,
>
> I have the following string,
> '/app/eclipse/2.1.3/eclipse' and need the following
> substring 'eclipse/2.1.3'
>
> Any hints on how I can do this?
>
> BR
>
> //Mikael


man cut --> -d -f
man awk --> FS
man bash ksh --> ${...#...}, ${...%...}

--
William Park, Open Geometry Consulting, <opengeometry@yahoo.ca>
Linux solution/training/migration, Thin-client
joe@invalid.address

2004-04-26, 2:34 pm

Petterson Mikael <mikael.petterson@era.ericsson.se> writes:

> I have the following string,
> '/app/eclipse/2.1.3/eclipse' and need the following
> substring 'eclipse/2.1.3'
>
> Any hints on how I can do this?


If your shell supports the syntax:

$ x=/app/eclipse/2.1.3/eclipse
$ y=${x%/*}
$ echo ${y#/*/}
eclipse/2.1.3

Joe
--
If people don't want to come out to the ballpark, nobody's going
to stop them.
- Yogi Berra
rakesh sharma

2004-04-26, 5:35 pm

Petterson Mikael <mikael.petterson@era.ericsson.se> wrote in message news:<408D102C.48FC985F@era.ericsson.se>...
> Hi,
>
> I have the following string,
> '/app/eclipse/2.1.3/eclipse' and need the following
> substring 'eclipse/2.1.3'
>
> Any hints on how I can do this?
>
> BR
>
> //Mikael


expr "X$string" : 'X/[a-z]*/\(.*\)/.*'

or,

sed -e 's:^/[^/]\{1,\}/\(.*\)/.*:\1:' <<[EOF]
$string
[EOF]
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com