parsing a string based on a character

parsing a string based on a character

am 30.01.2008 00:21:09 von vtcad

I would like to parse the string

/home/jsmith/output.gds

and have the output look like

$a=home
$b=jsmith
$c=output.gds

then parse $c, giving me
$d=output
$e=gds

thanks for helping a rookie.

Re: parsing a string based on a character

am 30.01.2008 02:12:05 von Janis Papanagnou

vtcad wrote:
> I would like to parse the string
>
> /home/jsmith/output.gds
>
> and have the output look like
>
> $a=home

Is the $ in front of the variables intentional? (A shell
assignment is written a=home instead of $a=home .)

> $b=jsmith
> $c=output.gds

{ IFS=/ read -r x a b c && printf "$%s\n" a=$a b=$b c=$c
} <<<"/home/jsmith/output.gds"

>
> then parse $c, giving me
> $d=output
> $e=gds

This one is an exercise for the reader. (Hint: use eval.)

Janis

>
> thanks for helping a rookie.

Re: parsing a string based on a character

am 30.01.2008 03:31:18 von vtcad

On Jan 29, 8:12 pm, Janis Papanagnou
wrote:
> vtcad wrote:
> > I would like to parse the string
>
> > /home/jsmith/output.gds
>
> > and have the output look like
>
> > $a=home
>
> Is the $ in front of the variables intentional? (A shell
> assignment is written a=home instead of $a=home .)
>
> > $b=jsmith
> > $c=output.gds
>
> { IFS=/ read -r x a b c && printf "$%s\n" a=$a b=$b c=$c
>
> } <<<"/home/jsmith/output.gds"
>
> > then parse $c, giving me
> > $d=output
> > $e=gds
>
> This one is an exercise for the reader. (Hint: use eval.)
>
> Janis
>
>
>
> > thanks for helping a rookie.

the $ was a guess at how I was supposed to identify variables.

Re: parsing a string based on a character

am 30.01.2008 09:41:07 von Stephane CHAZELAS

On Tue, 29 Jan 2008 15:21:09 -0800 (PST), vtcad wrote:
> I would like to parse the string
>
> /home/jsmith/output.gds
>
> and have the output look like
>
> $a=home
> $b=jsmith
> $c=output.gds
>
> then parse $c, giving me
> $d=output
> $e=gds
[...]

parse() {
set -f
IFS=/
set -- $1
a=$2 b=$3 c=$4
IFS=.
set -- $c
d=$1 e=$1
}

--
Stephane