Le jeu. 19 mars 2026 à 20:26, Pádraig Brady <[email protected]> a écrit :
>
> On 19/03/2026 15:24, Laurent Lyaudet wrote:
> > Hello,
> >
> > In some Bash scripts,
> > I make a call to "find" and then want to have all the lines of the
> > corresponding files,
> > prefixed with the corresponding file.
> > I created a hack with an always false regexp:
> > find ... | xargs grep -HP -v 'a(?!a)a'
> > A simpler solution would be:
> > find ... | xargs cat -H
> > But maybe you have something even better to propose.
> >
> > Best regards,
> > Laurent Lyaudet
>
> I do use this pattern quite a bit, but use grep '' to match everything.
>
> E.g. grep -H --color '' /proc/sys/net/ipv4/*
>
> Given this is already easy with grep,
> and grep has color highlighting wired up to it,
> I'm thinking this functionality is not needed in cat(1).
>
> cheers,
> Padraig
Thanks for your answer.
I don't remember if I tried to use an empty pattern with grep,
when I coded my solution to this problem.
That seems kind of a hack to me,
this is borderline between a true hack and an explicit feature.
That's why I prefer totally explicit solutions like cat -H
(or something else that -H
like --prefix-with-file-path
--prefix-with-line-number
or --line-format=line,":",file-path,";",line-number
--line-format=line,"$:",file-path,";",line-number
etc
)
to say that I don't want to filter out any line,
but only prefix them with something.
I totally understand your point of view,
where the goal is to avoid duplicated code here and there.
But personally, I prefer to think in terms of features clarity.
And as you can see with --line-format=,
I have a taste for the most powerful feature.
Note that it could be in a library used by all GNU Core Utils
that output lines to have a
--line-format=
option with keywords like line, file-path, etc.,
depending on the utility
mixed with arbitrary strings.
Maybe someday...
Best regards,
Laurent Lyaudet