Groups | Search | Server Info | Login | Register


Groups > comp.programming.contests > #24

Re: The First Pure Shell Contest (PUSH): relativepath

From Stephane CHAZELAS <stephane_chazelas@yahoo.fr>
Newsgroups comp.unix.shell, comp.unix.programmer, comp.programming.contests
Subject Re: The First Pure Shell Contest (PUSH): relativepath
Date 2011-08-21 20:12 +0000
Organization A noiseless patient Spider
Message-ID <slrnj52pld.nbc.stephane.chazelas@spam.is.invalid> (permalink)
References <9b7kg7F3njU1@mid.individual.net> <slrnj5050s.gg9.stephane.chazelas@spam.is.invalid> <87fwkuzw9r.fsf@sapphire.mobileactivedefense.com> <slrnj52c0v.d41.stephane.chazelas@spam.is.invalid>

Cross-posted to 3 groups.

Show all headers | View raw


2011-08-21, 16:19(+00), Stephane CHAZELAS:
> 2011-08-21, 15:14(+01), Rainer Weikusat:
>> Stephane CHAZELAS <Stephane.CHAZELAS@free.fr> writes:
>>> 2011-08-19, 17:23(+00), Jens Schweikhardt:
>>> [...]
>>>> Write a POSIX shell function named 'relativepath' which takes as
>>>> arguments two absolute canonicalized pathnames, and stores the relative
>>>> path from the first to the second in the variable "result" or "." when
>>>> the arguments are the same. Example: relativepath /foo/bar /foo/baz sets
>>>> result to "../baz".
>>> [...]
>>>
>>> In the not-answering-the-question category, what about:
>>>
>>>  printf '%s\n' "$1//$2/"|sed -e:1 -e'$!{N;b1' -e} -e'
>>>        s|/////*|///|;s|\(.*/\)\(.*/\)\1|\2|;:2
>>>            s|^[^/]*/\(\(.*/\)*/\)|\1../|;t2
>>>                 s|^/||;s|/$||;s/^$/./'
>>
>> It's a nice demonstration that some people really excel in the art of
>> creating complicated solutions to simple problems.
>
> It might look complicated as sed is not very verbose, but it's
> quite simple.
[...]

Actually, it can be simplified a bit:

$ cat rp
sed ':1
$!{N;b1
}
s|////*|///|;s|\(.*\)\(/.*/\)\1/|\2|;:2
s|/[^/]*\(.*//\)|\1../|;t2
s|/*||;s|/$||;s|^$|.|'<<.
$1//$2/
$ wc rp
  7   8 119 rp
$ ./rp /foo/bar /foo/baz
../baz

-- 
Stephane

Back to comp.programming.contests | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

The First Pure Shell Contest (PUSH): relativepath Jens Schweikhardt <schweikh@schweikhardt.net> - 2011-08-19 17:23 +0000
  Re: The First Pure Shell Contest (PUSH): relativepath Stephane CHAZELAS <stephane_chazelas@yahoo.fr> - 2011-08-19 18:13 +0000
    Re: The First Pure Shell Contest (PUSH): relativepath Jens Schweikhardt <usenet@schweikhardt.net> - 2011-08-19 19:12 +0000
      Re: The First Pure Shell Contest (PUSH): relativepath pk <pk@pk.invalid> - 2011-08-19 21:14 +0200
        Re: The First Pure Shell Contest (PUSH): relativepath Jens Schweikhardt <usenet@schweikhardt.net> - 2011-08-19 19:29 +0000
          Re: The First Pure Shell Contest (PUSH): relativepath pk <pk@pk.invalid> - 2011-08-19 21:37 +0200
            Re: The First Pure Shell Contest (PUSH): relativepath Jens Schweikhardt <usenet@schweikhardt.net> - 2011-08-19 20:04 +0000
              Re: The First Pure Shell Contest (PUSH): relativepath pk <pk@pk.invalid> - 2011-08-19 22:25 +0200
                Re: The First Pure Shell Contest (PUSH): relativepath Jens Schweikhardt <usenet@schweikhardt.net> - 2011-08-19 21:08 +0000
  Re: The First Pure Shell Contest (PUSH): relativepath Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-08-19 19:58 +0100
    Re: The First Pure Shell Contest (PUSH): relativepath Jens Schweikhardt <usenet@schweikhardt.net> - 2011-08-19 19:23 +0000
      Re: The First Pure Shell Contest (PUSH): relativepath Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-08-19 22:18 +0100
  Re: The First Pure Shell Contest (PUSH): relativepath Stephane CHAZELAS <Stephane.CHAZELAS@free.fr> - 2011-08-20 20:07 +0000
    Re: The First Pure Shell Contest (PUSH): relativepath Jens Schweikhardt <usenet@schweikhardt.net> - 2011-08-21 10:43 +0000
    Re: The First Pure Shell Contest (PUSH): relativepath Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-08-21 15:14 +0100
      Re: The First Pure Shell Contest (PUSH): relativepath Janis Papanagnou <janis_papanagnou@hotmail.com> - 2011-08-21 18:28 +0300
      Re: The First Pure Shell Contest (PUSH): relativepath Stephane CHAZELAS <stephane_chazelas@yahoo.fr> - 2011-08-21 16:19 +0000
        Re: The First Pure Shell Contest (PUSH): relativepath Stephane CHAZELAS <stephane_chazelas@yahoo.fr> - 2011-08-21 20:12 +0000
  Re: The First Pure Shell Contest (PUSH): relativepath Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-08-22 21:15 +0100
    Re: The First Pure Shell Contest (PUSH): relativepath Stephane CHAZELAS <stephane_chazelas@yahoo.fr> - 2011-08-23 18:23 +0000
      Re: The First Pure Shell Contest (PUSH): relativepath Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-08-23 19:52 +0100
        Re: The First Pure Shell Contest (PUSH): relativepath Stephane CHAZELAS <stephane_chazelas@yahoo.fr> - 2011-08-23 21:56 +0000

csiph-web