From: Hufnus on
I am trying to accomplish a path truncate in bash, something
like:

str1=/dir1/dir2/file; echo $str1 | sed 's/\/dir1//'

but I need "/dir1", for sed, to originate also as an input
variable. How can I process that functionality in a sed line?

thnaks for any hints
TonyB


--
__ __ _ I N C. http://www.sysdev.org
/ __|\\// __|| \ __ __ / tonyb(a)sysdev.org
\__ \ \/\__ \||)|/ O_)\/ / \/ System Tools / Utilities
|___/ || ___/|_ /\___|\_/ WIntel / Linux Device Drivers

From: Chris F.A. Johnson on
On 2006-09-01, Hufnus wrote:
> I am trying to accomplish a path truncate in bash, something
> like:
>
> str1=/dir1/dir2/file; echo $str1 | sed 's/\/dir1//'
>
> but I need "/dir1", for sed, to originate also as an input
> variable. How can I process that functionality in a sed line?

You don't need sed; use bash's parameter expansion:

printf "%s\n" "${str1%?"${str1#/*/}"}"


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
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
From: Hufnus on
On Thu, 31 Aug 2006 20:45:11 -0400
"Chris F.A. Johnson" <cfajohnson(a)gmail.com> wrote:

> On 2006-09-01, Hufnus wrote:
> > I am trying to accomplish a path truncate in bash, something
> > like:
> >
> > str1=/dir1/dir2/file; echo $str1 | sed 's/\/dir1//'
> >
> > but I need "/dir1", for sed, to originate also as an input
> > variable. How can I process that functionality in a sed
> > line?
>
> You don't need sed; use bash's parameter expansion:
>
> printf "%s\n" "${str1%?"${str1#/*/}"}"
>


Doesnt quite work, that just prints /dir1

I am trying to accomplish a path truncate to obtain the tail,
something like:

if str1=/dir1/dir2/file
where str2=/dir1, I need to obtain /dir2/file

thanks
Tony

--
__ __ _ I N C. http://www.sysdev.org
/ __|\\// __|| \ __ __ / tonyb(a)sysdev.org
\__ \ \/\__ \||)|/ O_)\/ / \/ System Tools / Utilities
|___/ || ___/|_ /\___|\_/ WIntel / Linux Device Drivers

From: Hufnus on
On Thu, 31 Aug 2006 17:37:48 -0800
Hufnus <tonyb(a)sysdev.org> wrote:

> On Thu, 31 Aug 2006 20:45:11 -0400
> "Chris F.A. Johnson" <cfajohnson(a)gmail.com> wrote:
>
> > On 2006-09-01, Hufnus wrote:
> > > I am trying to accomplish a path truncate in bash,
> > > something like:
> > >
> > > str1=/dir1/dir2/file; echo $str1 | sed 's/\/dir1//'
> > >
> > > but I need "/dir1", for sed, to originate also as an input
> > > variable. How can I process that functionality in a sed
> > > line?
> >
> > You don't need sed; use bash's parameter expansion:
> >
> > printf "%s\n" "${str1%?"${str1#/*/}"}"
> >
>
>
> Doesnt quite work, that just prints /dir1
>
> I am trying to accomplish a path truncate to obtain the tail,
> something like:
>
> if str1=/dir1/dir2/file
> where str2=/dir1, I need to obtain /dir2/file

Note that in the case str2=/dir2, I can live with either
of two answers:

a) returns /dir1/dir2/file
or,
b) returns /file

are equaly acceptable (user should have input str2=/dir1/dir2)
but prefer b).


TonyB

--
__ __ _ I N C. http://www.sysdev.org
/ __|\\// __|| \ __ __ / tonyb(a)sysdev.org
\__ \ \/\__ \||)|/ O_)\/ / \/ System Tools / Utilities
|___/ || ___/|_ /\___|\_/ WIntel / Linux Device Drivers

From: Chris F.A. Johnson on
On 2006-09-01, Hufnus wrote:
> On Thu, 31 Aug 2006 17:37:48 -0800
> Hufnus <tonyb(a)sysdev.org> wrote:
>
>> On Thu, 31 Aug 2006 20:45:11 -0400
>> "Chris F.A. Johnson" <cfajohnson(a)gmail.com> wrote:
>>
>> > On 2006-09-01, Hufnus wrote:
>> > > I am trying to accomplish a path truncate in bash,
>> > > something like:
>> > >
>> > > str1=/dir1/dir2/file; echo $str1 | sed 's/\/dir1//'
>> > >
>> > > but I need "/dir1", for sed, to originate also as an input
>> > > variable. How can I process that functionality in a sed
>> > > line?
>> >
>> > You don't need sed; use bash's parameter expansion:
>> >
>> > printf "%s\n" "${str1%?"${str1#/*/}"}"
>>
>> Doesnt quite work, that just prints /dir1

That's what you asked for.

>> I am trying to accomplish a path truncate to obtain the tail,
>> something like:
>>
>> if str1=/dir1/dir2/file
>> where str2=/dir1, I need to obtain /dir2/file
>
> Note that in the case str2=/dir2, I can live with either
> of two answers:
>
> a) returns /dir1/dir2/file

printf "%s\n" "$str1"

> or,
> b) returns /file

printf "%s\n" "/${str1##*/}"

> are equaly acceptable (user should have input str2=/dir1/dir2)

If you want to remove $str2 from the beginning of $str1:

printf "%s\n" "${str1#"$str2"}"


> but prefer b).

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
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