Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: PATCH: trailing components
- X-seq: zsh-workers 44424
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
- Subject: Re: PATCH: trailing components
- Date: Tue, 18 Jun 2019 16:42:07 +0200
- Cc: zsh-workers@xxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=CjVwNuOT7zSnqMf17ZeeTZro3zu8Dy9fbWkLNYg4YJ4=; b=syd3jE+mD2Mq7x/Fa7TVEWlgespVCfNnZBRucv5s4JnWQFJOOID0MWhmodmmmcY3ak 9AA7Y49fJYhTSTKRzo4AIZZ71yHCs/h68Qk8/oNeNtaTqx/xdmplFsL1kzpMRDsOUdMQ RzoZX55hg9NtVrl8TdKiaMsbVOqod8ozyou0K1fTdt/niMfC7rKIUfS0hSMaIABurk5X tMwlrWFkQzt/g2SO7TAMsQv0GrJ1vTiNsvLOyASECSw/2xeZ2YMufRx9vRdOAbQ9wtd5 Uywekr50JNELBnF0yDrPhWg9usam9cCXCqNNJ8bxuc2fE5TJpIGBrVjQnAZQkftx+dBa PsUg==
- In-reply-to: <523ba6f0-651a-444a-8ceb-c7c4ec186b20@www.fastmail.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-unsubscribe: <mailto:zsh-workers-unsubscribe@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <CGME20190618130008eucas1p176cc19c1c8b831fc30b4bf7b3294f3af@eucas1p1.samsung.com> <1560862806.7731.11.camel@samsung.com> <523ba6f0-651a-444a-8ceb-c7c4ec186b20@www.fastmail.com>
On 6/18/19, Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
> Peter Stephenson wrote on Tue, 18 Jun 2019 13:01 +00:00:
>> I needed to preserve a number of trailing components of a path (other
>> than one) and couldn't think of an easy way of doing it. Rather than
>> craft obscure pattern substitutions, or use an ad-hoc expression to
>> strip the prefix, I came up with a patch to add an optional number after
>> the "t" in history style modifiers to do this. I put it after rather
>> than before because history-style colon expressions are quite sensitive
>> to what the first character is after the colon.
>>
>> Before I come up with tests and fix up the ensuing failures, you can let
>> me know if there's a better way of doing this to save me the trouble.
>
> Here are some alternatives, but I'm not claiming any of them is better than
> «${foo:t2}»:
>
> f() {
> REPLY=
> for 1 in {1..$1} ; do REPLY=$2:t/$REPLY; 2=$2:h ; done
> }
> f $N "$foo"; foo=$REPLY
>
> or
>
> foo=${(j./.)${${(s./.)foo}[-$N,-1]}}
> (plus or minus handling of runs of several consecutive slashes)
>
> or
>
> tmp=$foo
> for i in {1..$N} ; do tmp=$tmp:h ; done
> foo=${foo#$tmp/*}
Here's one (two) more, excluding (including) empty segments in the count,
% a=a/b/c/d///e/g
% echo ${(M)a%%([^/]#(/|)##)(#c5)}
b/c/d///e/g
% echo ${(M)a%%([^/]#(/|))(#c5)}
d///e/g
--
Mikael Magnusson
Messages sorted by:
Reverse Date,
Date,
Thread,
Author