From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13490 invoked by alias); 11 Oct 2015 23:05:25 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 20738 Received: (qmail 15312 invoked from network); 11 Oct 2015 23:05:22 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.0 X-Authority-Analysis: v=2.1 cv=X+5rdgje c=1 sm=1 tr=0 a=TDXRqai3AtB4qfuLBL55xA==:117 a=TDXRqai3AtB4qfuLBL55xA==:17 a=N659UExz7-8A:10 a=DYyVxQHcOC4irsze7rAA:9 a=Ecq5sBAuiouX4xHA:21 a=siZ42aVA_5t3_ZbI:21 a=pILNOxqGKmIA:10 Message-id: <561AEB2F.8030808@eastlink.ca> Date: Sun, 11 Oct 2015 16:05:19 -0700 From: Ray Andrews User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Icedove/31.7.0 MIME-version: 1.0 To: zsh-users@zsh.org Subject: Re: subsitutions and beginning of lines. References: <561AB49A.4060801@eastlink.ca> <20151011210902.566de251@ntlworld.com> In-reply-to: <20151011210902.566de251@ntlworld.com> Content-type: text/plain; charset=windows-1252; format=flowed Content-transfer-encoding: 7bit On 10/11/2015 01:09 PM, Peter Stephenson wrote: > On Sun, 11 Oct 2015 12:12:26 -0700 > Ray Andrews wrote: >> ${var// /} >> >> When doing that sort of substitution, is is possible to test for newlines? >> I'm playing with a function that grabs history and I'm trying to strip >> off the leading numbers. At one point I have the output captured in a >> variable and various forms of substitution come close, but the substitution >> treats the entire variable as one entity whereas I want the substitution >> to be performed fresh on each new line of output. I can't find any >> specific newline character. OTOH if the variable was an array there'd >> probably be some way of processing each line as a separate entity, which >> would serve fine. I know I'm close, but I can't quite bag it. > If I'm following, you are indeed nearly there: you need to split the > variable to an array on newlines, while not splitting on anything else. > To do that, you quote the array, then force the splitting which ensures it > only gets split on what you tell it and not other other random spaces: > > var="1:one two > 2:three four" > print -l -- ${(f)"${var}"} > > The -l prints one element per line for clarity. > > You've now got the lines you want as if they were array elements. You > can then surround that substitution with the sort you first thought of. > For example, to strip the "number:", > > print -l -- ${${(f)"${var}"}##<->:} > > where, as you now know, <-> matches any set of digits. > > pws Thanks Peter, I just came up with this: echo "${(F)var[@]//#???????/}" ... it seems the numeric leader is always the same width, so mine is more stupidly linear, but it was my enlightenment as to '<->' that got me going with this. I pretty much understand it; the trick was to get the substitution on each element. And get the newline as the separator. I'll study your version too. BTW, I just rediscovered your: A User's Guide to the Z-Shell Peter Stephenson 2003/03/23 In the fog of war I'd considered it to be 'the manual' back when I first started, then when I started reading the real manual I had this powerful sensation that the manual had gone downhill. Now I'm realizing that they're not the same tome after all and I must say that yours is a pleasure to read which can't be said of the manual. Is there an update?