From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,NICE_REPLY_A,RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 15275 invoked from network); 21 Jan 2022 08:51:22 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 21 Jan 2022 08:51:22 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1642755082; b=hWppvjWT/4xEwg2upMSXnJn/+IqeC49K5BpfO6/BzkG2PJ2oFb/+juK53A4tmGagk7V6B2t8sD /Wkv0KBslwMTbWPWwCeBWEQojGJxgBX5p7v3BUla49C4kiijHxIxrgTwUvVUM0CU9TSqmWqjpz FSwba2PC32faaNboqmBpTGjKu9gKQ89k+QLsAW9Q8YpaenItWbpocPiCUFBJ+fWTpl77x3+xyK fuDBsUAlK7srAHqfa32hrk4XiV2b8MYSsjLBLKSJIFXRIuETqrFcfu40FjjgMQvd5l3f/EW4V7 SOl5l6aClaGq45YMwbPjPPe7mHAW7aZrsSxk8984LBttBA==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (p3plsmtp27-06-2.prod.phx3.secureserver.net) smtp.remote-ip=216.69.139.56; dmarc=none header.from=rayninfo.co.uk; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1642755082; bh=kM6XmUZSTdLRc0TFSdTE1mgmiy2vQjr8xIxQ5dhG/TM=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:Content-Type:In-Reply-To: MIME-Version:Date:Message-ID:From:References:To:Subject:DKIM-Signature; b=GKPmlvJ2dO+qXvLiK4q0By7DVzKdnBEzXO+igDpKbycSPzJfcL+zxGwWuBpJygHFSbOq6f1lWs SRF7Hh1uQzjkK/Nr7iHyZcoOd97VhCiZl/+PRTkBnDl3NT8fXrmtefzKe7F/HMzhMC+tH1lUWx GM2J4DELVQqr6ocEedl2DhR1ahvHbrZh2GUhDCOCRQFQtaF4eRHgiPmQ7pUgyVjTLSMcOb/AKS MT5KfQws48EoSQBdU2Xc4PMni9m07eksdyPiUYboZTI5Gv4Cjb7XRMsV9NSRhj4AOJdiaUY77p La282Sk423FWq69JC05+vMhv+HlOfpXtXGsJYL9yO4qBkg==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Transfer-Encoding: Content-Type:In-Reply-To:MIME-Version:Date:Message-ID:From:References:To: Subject:Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=hc7RYFpSw2WJ5p8Qdrekm+pxb2AJftjKpdDrumkrfPQ=; b=BZGuwlzwUpYeiZE8b4MXopSBqm aKKD16zsIEzcjNpWMUsDEMQI7Mn9KivB7m1D1meD89evEhkaW8Eeqz5lta+H8Tvl33iFHPbRL2wOm +P9LWUQzYVAnuB6sZSEcGbemx3nY87G21fywZbNXG6EzS4U6qJHhNnlpzLBNQQZzWRuLe5vHTZvMx 0zexHNO2pFgS5w1e/ZXby84IgCz2vaju9QGhpz44yRr6MvIMAk/65Q3tdCgHmwC9OPYd+Ctfv06vI +kuMj65ef8fdI04IWwhUR8+E7kOAHJK4DTOj8s7fVwgIevDeGzfhBWdja2qdMHht8D/Y0wjgdgapE SmxfkqBw==; Received: from authenticated user by zero.zsh.org with local id 1nApdh-000Fhc-Gc; Fri, 21 Jan 2022 08:51:21 +0000 Authentication-Results: zsh.org; iprev=pass (p3plsmtp27-06-2.prod.phx3.secureserver.net) smtp.remote-ip=216.69.139.56; dmarc=none header.from=rayninfo.co.uk; arc=none Received: from p3plsmtp27-06-2.prod.phx3.secureserver.net ([216.69.139.56]:56382 helo=p3plwbeout27-06.prod.phx3.secureserver.net) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1nApcx-000F06-Ur; Fri, 21 Jan 2022 08:50:37 +0000 Received: from mailex.mailcore.me ([94.136.40.144]) by :WBEOUT: with ESMTP id ApcunMYG1nzGHApcvnRQzy; Fri, 21 Jan 2022 01:50:33 -0700 X-CMAE-Analysis: v=2.4 cv=OsmKdwzt c=1 sm=1 tr=0 ts=61ea73d9 a=wXHyRMViKMYRd//SnbHIqA==:117 a=z1LKIbrbDHtrqARf1MiRIA==:17 a=iVWrzo99Z24A:10 a=IkcTkHD0fZMA:10 a=DghFqjY3_ZEA:10 a=pGLkceISAAAA:8 a=q2GGsy2AAAAA:8 a=22Ro6XbQAAAA:8 a=FDqriS-ZUK_z87bjFKMA:9 a=QEXdDO2ut3YA:10 a=z9dJwno5l634igLiVhy-:22 a=p1RiBf0vZFgkX3Jmbd9N:22 X-SECURESERVER-ACCT: david@rayninfo.co.uk X-SID: ApcunMYG1nzGH Received: from host86-157-95-82.range86-157.btcentralplus.com ([86.157.95.82] helo=[192.168.1.231]) by smtp10.mailcore.me with esmtpa (Exim 4.94.2) (envelope-from ) id 1nApcu-0002Fy-CD for zsh-users@zsh.org; Fri, 21 Jan 2022 08:50:32 +0000 Subject: Re: zsh-ify a bash script To: zsh-users@zsh.org References: <2e3d8557-9f6f-5d17-7517-7f9fcf7be1ce@rayninfo.co.uk> <20220121074811.GA22276@tarpaulin.shahaf.local2> From: zzapper Message-ID: Date: Fri, 21 Jan 2022 08:50:32 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: <20220121074811.GA22276@tarpaulin.shahaf.local2> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Mailcore-Auth: 8308408 X-Mailcore-Domain: 840527 X-123-reg-Authenticated: david@rayninfo.co.uk X-Originating-IP: 86.157.95.82 X-CMAE-Envelope: MS4xfFcC90JcNrni4kJumlGGOo2F0CszhyCV6gHiwSPYEvXzo77LCuMsf0iGQMq3S1As/mAOPHcDWeZk40B962L3aibEGty99DCJciAWVuMLEeH2HXhfvx2f eLjaESI4mYbWVPJGBrKjCfN2xmwZYG+fm8F21Plxu8DLMSdPWzu2DwZ7FeYbrTynFhjaGwaP2Qyb9A== X-Seq: 27485 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: On 21/01/2022 07:48, Daniel Shahaf wrote: > Andreas Kusalananda Kähäri wrote on Thu, Jan 20, 2022 at 12:26:06 +0100: >> On Thu, Jan 20, 2022 at 11:40:56AM +0100, Mikael Magnusson wrote: >>> On 1/20/22, Mikael Magnusson wrote: >>>> On 1/20/22, Bart Schaefer wrote: >>>>> On Wed, Jan 19, 2022 at 12:57 PM zzapper wrote: >>>>>> Dumb CLI trick. Wanted to find files containing all of several terms >>>>>> (dup2, pledge, socketpair, fork), but they could occur anywhere in the >>>>>> file: >>>>> Not zsh any more than the first example, but instead of "grep -l" on >>>>> the entire file contents for each term ... >>>>> >>>>> # start by getting the actual occurrences of all the terms: >>>>> find . -name '*.c' | xargs egrep >>>>> '\<(dup2|pledge|fork|socketpair.*SOCK_STREAM)\>' /dev/null | >>>>> # reduce the results to just file names and search terms: >>>>> sed -E 's/(^[^:]*\.c:).*\<(dup2|pledge|fork|socketpair)\>.*/\1\2/' | >>>>> # make every search term unique per file: >>>>> sort -u | >>>>> # discard the search terms, leaving only file names: >>>>> cut -d : -f 1 | >>>>> # count the number of times each file name appears: >>>>> uniq -c | >>>>> # print names with a count of 4 (the number of search terms): >>>>> sed -nE 's/^ *4 //p' >>>>> >>>>> Adjusting this for edge cases where two search terms appear on the >>>>> same line is left as an exercise. >>>> This part would probably be fixed by passing -o to grep (didn't test >>>> in the above but): >>>> % echo foobar|grep -E '(foo|bar)' >>>> foobar >>>> % echo foobar|grep -oE '(foo|bar)' >>>> foo >>>> bar >>> I guess this is more or less the same solution, >>> % grep -Eo '(zsfree|zerr|subst)' Src/**/*.c|sort -u|sed 's/:[^:]*'//|uniq -c|grep -E '^\s+3' >>> 3 Src/Zle/zle_main.c >>> 3 Src/builtin.c >>> 3 Src/exec.c >>> 3 Src/glob.c >>> 3 Src/hist.c >>> 3 Src/jobs.c >>> 3 Src/signals.c >>> 3 Src/subst.c >>> 3 Src/utils.c >>> >>> >> Awk-ifying the end of that pipeline... and just playing around with the >> grep a bit for fun (changed to use BREs, but still non-standard due to >> -w and -o). >> >> grep -Fwo -e zsfree -e zerr -e subst -- Src/**/*.c | >> awk -F: '!seen[$0]++ && ++count[$1] == 3 { print $1 }' >> >> This obviously assumes that no pathname contains colons or embedded >> newlines. > Using glob qualifiers: > > % has() { <$REPLY grep -q -- "$1" } > % print -raC1 -- **/*.c(e*has zerr*e*has zsfree*e*has subst*) > Src/Zle/zle_main.c > Src/builtin.c > Src/exec.c > Src/glob.c > Src/hist.c > Src/jobs.c > Src/signals.c > Src/subst.c > Src/utils.c > % # looks good just trimmed for my system (Mint) has() { <$REPLY grep -q -- "$1" } print -raC1 -- /usr/[sS]rc/**.c(e*has stdio*e*has fcntl*e*has mman*) H ### BTW is there any library of text files we can all guarantee on having for future test purposes?