On 2023-10-10, Joerg M. Sigle wrote:
> Hello GNU grep team
> 
> I have grep (GNU grep) 3.6 on a Debian / Devuan Linux system.
> 
> Recently I noticed that when using the -o switch, grep also shows
> the filename for each hit in front of the desired output, and
> a ":" as separator.
> IIRC this was not the case in previous versions of grep [but
> please correct me if I'm wrong here].
> 
> In any case, the filename: is NOT shown before matching content
> when the -o switch is not used.

This depends on the number of files being searched and on the
presence of the -h or -H switch.  It should not be affected by the
-o switch.  I tested the few versions I have on hand, 3.4, 3.7 and
3.11, and they all behave according to the man page with respect to
the -o flag and the number of files searched.

> According to the manual, the -o switch should only limit output to
> matching parts, and NOT ADDITIONALLY activate the output of
> filename:
> 
> -o, --only-matching
>       Print only the matched (non-empty) parts of a matching line,
>       with each such part on a separate output line.
> 
> 
> N.B.:
> Showing the filename ONLY instead of matching lines/only matching
> part) IS already controlled with the -l switch.
> So a different switch would be needed to show the filename IN
> ADDITION to the output of -o.
> I think, somebody thought they'd do something beneficial by
> showing the filename in front of the output of -o, but I'm afraid
> they are not.
> And I'm also afraid that this needs some workaround to remove the
> filename again for many applications of grep,
> so changing back to the expected behaviour might cause trouble
> again, of course.
> 
> Example:
> 
> $ grep  -o PV1.... *.hl7
> 
> Currently produced output (supposedly wrong):
> 
> PAT_20231010_112021_010166.hl7:PV1|1|O
> PAT_20231010_112021_010167.hl7:PV1|1|O
> PAT_20231010_112420_010168.hl7:PV1|1|I
> PAT_20231010_112421_010169.hl7:PV1|1|I
> PAT_20231010_112421_010170.hl7:PV1|1|I
> ...
> 
> Desired output (supposedly right):
> PV1|1|O
> PV1|1|O
> PV1|1|I
> PV1|1|I
> PV1|1|I
> ...

Grep is behaving correctly, according to this part of the man page:

    -H, --with-filename
           Print the file name for each match.  This is the default
           when there is more than one file to search.  This is
           a GNU extension.

    -h, --no-filename
           Suppress the prefixing of file names on output.  This is
           the default when there is only one file (or only standard
           input) to search.

Regards,
Gary




Reply via email to