zsh-users
 help / color / mirror / code / Atom feed
From: "Andreas Kusalananda Kähäri" <andreas.kahari@abc.se>
To: Mikael Magnusson <mikachu@gmail.com>
Cc: Bart Schaefer <schaefer@brasslantern.com>,
	zzapper <zsh@rayninfo.co.uk>, Zsh-Users List <zsh-users@zsh.org>
Subject: Re: zsh-ify a bash script
Date: Thu, 20 Jan 2022 12:26:06 +0100	[thread overview]
Message-ID: <YelGzjPQBp8lquJp@box> (raw)
In-Reply-To: <CAHYJk3Ti732-NjPwT8sgiLW9p81xN_mvmOHcWzJcMdWR3RH-iw@mail.gmail.com>

On Thu, Jan 20, 2022 at 11:40:56AM +0100, Mikael Magnusson wrote:
> On 1/20/22, Mikael Magnusson <mikachu@gmail.com> wrote:
> > On 1/20/22, Bart Schaefer <schaefer@brasslantern.com> wrote:
> >> On Wed, Jan 19, 2022 at 12:57 PM zzapper <zsh@rayninfo.co.uk> 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
> 
> 
> -- 
> Mikael Magnusson

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.

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.


  reply	other threads:[~2022-01-20 11:26 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-19 20:56 zzapper
2022-01-20  4:12 ` Bart Schaefer
2022-01-20 10:31   ` Mikael Magnusson
2022-01-20 10:40     ` Mikael Magnusson
2022-01-20 11:26       ` Andreas Kusalananda Kähäri [this message]
2022-01-20 18:31         ` Bart Schaefer
2022-01-20 22:13           ` Andreas Kusalananda Kähäri
2022-01-21  7:48         ` Daniel Shahaf
2022-01-21  8:50           ` zzapper

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YelGzjPQBp8lquJp@box \
    --to=andreas.kahari@abc.se \
    --cc=mikachu@gmail.com \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    --cc=zsh@rayninfo.co.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).