GNU bug report logs -
#66434
grep recently shows the filename with -o , but it shouldn't.
Previous Next
To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 66434 in the body.
You can then email your comments to 66434 AT debbugs.gnu.org in the normal way.
Toggle the display of automated, internal messages from the tracker.
Report forwarded
to
bug-grep <at> gnu.org
:
bug#66434
; Package
grep
.
(Tue, 10 Oct 2023 10:59:01 GMT)
Full text and
rfc822 format available.
Acknowledgement sent
to
"Joerg M. Sigle" <joerg.sigle <at> jsigle.com>
:
New bug report received and forwarded. Copy sent to
bug-grep <at> gnu.org
.
(Tue, 10 Oct 2023 10:59:01 GMT)
Full text and
rfc822 format available.
Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):
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.
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
...
Thanks and kind regards, Joerg Sigle
--
-------------------------------------------------------------------
Dr. med. Jörg M. Sigle +41 76 276 86 94
http://www.ql-recorder.com +41 32 510 23 46
http://www.jsigle.com +49 176 96 43 54 13
Information forwarded
to
bug-grep <at> gnu.org
:
bug#66434
; Package
grep
.
(Tue, 10 Oct 2023 16:15:02 GMT)
Full text and
rfc822 format available.
Message #8 received at submit <at> debbugs.gnu.org (full text, mbox):
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
Information forwarded
to
bug-grep <at> gnu.org
:
bug#66434
; Package
grep
.
(Tue, 10 Oct 2023 21:13:01 GMT)
Full text and
rfc822 format available.
Message #11 received at 66434 <at> debbugs.gnu.org (full text, mbox):
Gary, thank you very much for your helpful response.
Sorry I failed to see this GNU extension/default and the -H and -h switches even though I looked :-)
Maybe a hint in the man entry for -o might be good; e.g. "Also see: -h, -H".
In hindsight, I also understand the effect of a peculiarity of HL7 files in this context:
I was grepping HL7 files. The HL7 standard uses only CR (hex 0D) to separate HL7 segments.
WITHOUT -o, grep results typically span multiple HL7 segments.
And CR means carriage return, so a filename displayed by grep would immediately be covered by content from the next segment.
WITH -o and a short search string, however, grep results remain within one HL7 segment.
So NO CR appears in the output. So the filename displayed by grep remains visible.
This caused my (false) impression that in GNU grep, the -o would also trigger the -H with it.
And left me sufficiently puzzled to send this error report.
Of course, a closer inspection of grep output without -o could have revealed this!
I hope that documenting this here might help others avoid the same little trap :-)
Thanks & kind regards! Joerg
Added tag(s) notabug.
Request was from
Paul Eggert <eggert <at> cs.ucla.edu>
to
control <at> debbugs.gnu.org
.
(Tue, 17 Oct 2023 19:04:02 GMT)
Full text and
rfc822 format available.
bug closed, send any further explanations to
66434 <at> debbugs.gnu.org and "Joerg M. Sigle" <joerg.sigle <at> jsigle.com>
Request was from
Paul Eggert <eggert <at> cs.ucla.edu>
to
control <at> debbugs.gnu.org
.
(Tue, 17 Oct 2023 19:04:02 GMT)
Full text and
rfc822 format available.
bug archived.
Request was from
Debbugs Internal Request <help-debbugs <at> gnu.org>
to
internal_control <at> debbugs.gnu.org
.
(Thu, 16 Nov 2023 12:24:09 GMT)
Full text and
rfc822 format available.
This bug report was last modified 1 year and 294 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.