zsh-users
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: zzapper <zsh@rayninfo.co.uk>, Zsh-Users List <zsh-users@zsh.org>
Subject: Re: zsh-ify a bash script
Date: Thu, 20 Jan 2022 11:31:35 +0100	[thread overview]
Message-ID: <CAHYJk3S16KVmxBd0acZvM+_UYAXq+KHBviMxOQ1VDkGx0-PFRw@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7bEVVC9pFB3dnWXFJnAvmhHTokFoMWB_oCuc+6u=HpvKw@mail.gmail.com>

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

(sorry to spoil the exercise)

> If you have file names with colons
> in them, the first set of parens in the first sed will need a more
> precise RE.


-- 
Mikael Magnusson


  reply	other threads:[~2022-01-20 10:32 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 [this message]
2022-01-20 10:40     ` Mikael Magnusson
2022-01-20 11:26       ` Andreas Kusalananda Kähäri
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=CAHYJk3S16KVmxBd0acZvM+_UYAXq+KHBviMxOQ1VDkGx0-PFRw@mail.gmail.com \
    --to=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).