Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: zsh-ify a bash script

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)'
% echo foobar|grep -oE '(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