Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: zsh-ify a bash script
- X-seq: zsh-users 27479
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Cc: zzapper <zsh@xxxxxxxxxxxxxx>, Zsh-Users List <zsh-users@xxxxxxx>
- Subject: Re: zsh-ify a bash script
- Date: Thu, 20 Jan 2022 11:31:35 +0100
- Archived-at: <https://zsh.org/users/27479>
- In-reply-to: <CAH+w=7bEVVC9pFB3dnWXFJnAvmhHTokFoMWB_oCuc+6u=HpvKw@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- References: <2e3d8557-9f6f-5d17-7517-7f9fcf7be1ce@rayninfo.co.uk> <CAH+w=7bEVVC9pFB3dnWXFJnAvmhHTokFoMWB_oCuc+6u=HpvKw@mail.gmail.com>
On 1/20/22, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Wed, Jan 19, 2022 at 12:57 PM zzapper <zsh@xxxxxxxxxxxxxx> 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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author