Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Stephane CHAZELAS Newsgroups: comp.unix.shell,comp.unix.programmer,comp.programming.contests Subject: Re: The First Pure Shell Contest (PUSH): relativepath Date: Sun, 21 Aug 2011 20:12:29 +0000 (UTC) Organization: A noiseless patient Spider Lines: 43 Message-ID: References: <9b7kg7F3njU1@mid.individual.net> <87fwkuzw9r.fsf@sapphire.mobileactivedefense.com> Injection-Date: Sun, 21 Aug 2011 20:12:29 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="qzG0Li1iwhS8BRIL3GjZtw"; logging-data="26454"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18HOauWEEsKB9iqM+s1rMETSQGy2mMnAqld6CdJVNCqlA==" User-Agent: slrn/pre1.0.0-18 (Linux) Cancel-Lock: sha1:RJMNaL2NZNW4fhgk/qZ59dTAqx0= Mail-Copies-To: nobody Xref: x330-a1.tempe.blueboxinc.net comp.unix.shell:1780 comp.unix.programmer:1176 comp.programming.contests:24 2011-08-21, 16:19(+00), Stephane CHAZELAS: > 2011-08-21, 15:14(+01), Rainer Weikusat: >> Stephane CHAZELAS 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