Skip to content

grep - -v -o with -A/-B/-C prints nothing; GNU prints the matches found in the context lines #209

Open

Description

@sylvestre

With -v -o, the selected lines are the non-matching ones, so they have nothing to print.
When context is also requested, GNU grep prints, for each context line (a line that does
match), its matching parts with the context separator - (2-dog), plus -- between
non-adjacent groups. uu_grep drops the context entirely whenever -o is given, so it
prints nothing.

Related: #175 / PR #188 restore the -- separators for -o with context, but keep skipping
context lines, so they do not cover this case.

Exposed by the GNU test yesno (tests #24 -v -C 1 -o and #38 -m 3 -v -C 1 -o). The other
yesno failures are #175 (tests #4, #20) and #156 (--group=XYZ, --no-gr, tests #5, #6,
#8, #9, #25, #26).

Reproduction

$ printf 'cat\ndog\ncat\ncat\ncat\ndog\n' > pets

Rust (incorrect)

$ ./target/release/grep -n -v -A 1 -o dog pets
# Output: (none)
# Exit code: 0

GNU (correct)

$ LC_ALL=C /usr/bin/grep -n -v -A 1 -o dog pets
2-dog
6-dog
# Exit code: 0

Test

#[test]
fn invert_only_matching_prints_context_matches() {
    let (_s, mut c) = ucmd();
    c.args(&["-n", "-v", "-B", "1", "-o", "ant"])
        .pipe_in("ant\nbee\nwasp\nbee\nant\n")
        .succeeds()
        .stdout_only("1-ant\n");
}

Exposed by the GNU grep test suite: yesno.

Activity

  1. added
    bug
    good first issue
    gnu-compat
    on Nov 17, 2026
  2. added a commit that references this issue on Nov 25, 2026
    ab89d1d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggnu-compatgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions