From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23310 invoked by alias); 30 Jan 2016 16:54:14 -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: 21210 Received: (qmail 519 invoked from network); 30 Jan 2016 16:54:13 -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,T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=from:message-id:date:in-reply-to:comments:references:to:subject :mime-version:content-type; bh=QtT4Z/xlN6WnFEjk9hF04CvaugKuT1OJlU26PIUs1dI=; b=PuoHpJe5Viafdd7HcXoAi7J/hlpEZlvRLsdCkBb63xYgdLNYl7spfBiWTxuTJcAT1c YV0OjBFtztdwCvwF6uJpy2QBThEpgFZvUrCx4GP8eNl3NtyoWpTWg2WEMT94h4XJnuqj WGZ2+SfHb9LkM8IhBJo+aMxKveH63NLwQoBhiYY8WSlOLlPye7S6VW4Ka1r0YQlvJqyh 9pTbDVKR3QfieGygjtk28Z5NqOsXyEOlEBSqByxta2CLGCoHFfFGEgcv53ifBsD26uMU fwero1RAJgFI5PauYlsRrBcZIjBC21TJYdWO1lR6GukfleAtMsoaWToEWGIHqdfTbDgi 820g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:message-id:date:in-reply-to:comments :references:to:subject:mime-version:content-type; bh=QtT4Z/xlN6WnFEjk9hF04CvaugKuT1OJlU26PIUs1dI=; b=Z0HZjAphnSUPQ+0zPyTBi58ERT7WiMJZwTCrhx7tBojuPwkqMtk6lMzrE3Ox+cIl+i SGiHGGa/V5KQpgwYF1zH0SHfqWTG3xBmnJ1NWJ0R3/gVFsPEg8uQdccYhyYOZIYHgFob GSHcmi6K9pbHYd6An2kKGCx0NgxCcVayxmnEZHHMD1K42Gn+XDMkwg/bsjquqMc+f3gN szp9VHCOnie5Ms2ecST6VIZHFFR9mi2yJafA5Fi2r7niYNhxyWaxf28/AGSyk9fTGV1j BcoMHif/M3zUmYYCJHVRybY57Hqe0Yv4IltOR1NGZu7gwUm/n9QHbOQdgL/wTLlJfU9O 3+7A== X-Gm-Message-State: AG10YOSV20FDOVzazQW/gAGXmvHMI/wyFag9FRN+KxcNQSZQtHkDWuWTI6BBbAcjbOFNmA== X-Received: by 10.98.93.136 with SMTP id n8mr23481745pfj.88.1454172847146; Sat, 30 Jan 2016 08:54:07 -0800 (PST) From: Bart Schaefer Message-Id: <160130085456.ZM9730@torch.brasslantern.com> Date: Sat, 30 Jan 2016 08:54:56 -0800 In-Reply-To: Comments: In reply to Sebastian Gniazdowski "Re: Feature request (@M):# with context matches" (Jan 30, 1:13pm) References: X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: Zsh Users Subject: Re: Feature request (@M):# with context matches MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii On Jan 30, 1:13pm, Sebastian Gniazdowski wrote: } } However, grep -C is not very possible to replace with Zsh code. I'm a little puzzled about how you think this would work in a parameter substitution context. Grep operates on a stream of input lines, parameter substitution works on values in memory. Do you propose to capture the entire input in an array before even performing the search? In any case the way you want to do this kind of thing is with array subscript flags. r Reverse subscripting: if this flag is given, the EXP is taken as a pattern and the result is the first matching array element, substring or word (if the parameter is an array, if it is a scalar, or if it is a scalar and the `w' flag is given, respectively). The subscript used is the number of the matching element, so that pairs of subscripts such as `$foo[(r)??,3]' and `$foo[(r)??,(r)f*]' are possible if the parameter is not an associative array. R Like `r', but gives the last match. For associative arrays, gives all possible matches. i Like `r', but gives the index of the match instead; this may not be combined with a second argument. I Like `i', but gives the index of the last match, or all possible matching keys in an associative array. (The docs have a lot more, I just copied the critical bits.) So you want something like while (( ( hit = $list[(i)*$search_pattern*] ) < $#list )) do print -r -- $list[hit-3,hit+3] shift $hit list done with some appropriate checking that (hit-3) doesn't become negative and wrap around to the other end of the array, and other foo to manage the possiblity of overlapping contexts. Note you don't even need the tilde in $search_pattern, [(i)...] forces everything into pattern context; if you do NOT want patterns, you need ${(b)search_pattern} (or in older zsh ${(q)search_pattern} will usually work).