bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

--only-matching interacts badly with context lines and line head [bug-gr


From: Charles Levert
Subject: --only-matching interacts badly with context lines and line head [bug-grep]
Date: Tue, 22 Feb 2005 23:19:55 -0500
User-agent: Mutt/1.4.1i

While I have the whole src/grep.c prline() code in my
brain's "L1 cache", I found another set of issues with
-o/--only-matching.

Please study the following and join me for discussion
on the other side of it.  Look for the "bash$ " prompts,
not necessarily at the beginning of a line.


========================================
bash$ 
bash$ cat foo.txt
One
Two
foo match bar match baz
Four
Five
Six
Foo match Bar match Baz
Eight
Nine
Ten
FOO match BAR match BAZ
Eleven
Twelve
bash$ 
bash$ grep -o match foo.txt
match
match
match
match
match
match
bash$ 
bash$ grep -Ho match foo.txt
foo.txt:match
match
foo.txt:match
match
foo.txt:match
match
bash$ 
bash$ grep -Hno match foo.txt
foo.txt:3:match
match
foo.txt:7:match
match
foo.txt:11:match
match
bash$ 
bash$ grep -Hnbo match foo.txt
foo.txt:3:8:match
match
foo.txt:7:46:match
match
foo.txt:11:85:match
match
bash$ 
bash$ grep -o1 match foo.txt
match
match
--
match
match
--
match
match
bash$ 
bash$ grep -Ho1 match foo.txt
foo.txt-foo.txt:match
match
foo.txt---
foo.txt-foo.txt:match
match
foo.txt---
foo.txt-foo.txt:match
match
foo.txt-bash$ 
bash$ 
bash$ grep -Hno1 match foo.txt
foo.txt-2-foo.txt:3:match
match
foo.txt-4---
foo.txt-6-foo.txt:7:match
match
foo.txt-8---
foo.txt-10-foo.txt:11:match
match
foo.txt-12-bash$ 
bash$ 
bash$ grep -Hnbo1 match foo.txt
foo.txt-2-4-foo.txt:3:8:match
match
foo.txt-4-32---
foo.txt-6-42-foo.txt:7:46:match
match
foo.txt-8-70---
foo.txt-10-81-foo.txt:11:85:match
match
foo.txt-12-109-bash$ 
bash$ 
========================================


AFAIK, -o/--only-matching is a GNU extension and its
behavior isn't bound by any specification such as POSIX.
Please correct me if I'm wrong.


First, it is obvious that --only-matching does not agree
with context lines.  I propose the following wholesale
fix in main():

  if (only_matching)
    out_after = out_before = 0;

as context lines do not contain any match by definition.


Second, if any line head is specified (filename
[-H/--with-filename], line [-n/--line-number], byte
[-b/--byte-offset]) and there are several matches in one
line, that line head is printed only once before the first
match and other matches begin on a new line.  There is
potential for ambiguity if a match can itself begin with
something similar to what has been specified as line head.
I propose that a line head, if any is specified, be printed
before each match.


Third, assuming the previous proposal is retained, I
propose that combining --only-matching and --byte-offset
print the byte offset of every match itself, as opposed
to the byte offset of the beginning of the line on which
each match appears.  I think this would be very useful
and would actually be what the user would expect and
naturally interpret when combining these options and
viewing the results.


I plan to update my patch at

  <https://savannah.gnu.org/patch/index.php?func=detailitem&item_id=3644>

to implement these proposals, since these affect mostly the
same lines that are already heavily modified by the patch.
It will always remain possible to ignore this specific
version of the patch and use a previous one.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]