Skip to content

grep: print the group separator with -o and context options - #188

Open
sarankumar1325 wants to merge 1 commit into
uutils:mainfrom
sarankumar1325:grep-only-matching-group-separator
Open

sarankumar1325 wants to merge 1 commit into
uutils:mainfrom
sarankumar1325:grep-only-matching-group-separator

Conversation

@sarankumar1325

Copy link
Copy Markdown
Contributor

Fixes #175

With -o, GNU grep doesn't print context lines, but it still tracks the context groups and prints the -- separator (or --group-separator) between groups that aren't adjacent. uutils grep zeroed the context and cleared has_context whenever -o was given, so the separator never appeared:

$ printf 'foo 1\nline 2\nline 3\nfoo 4\nline 5\n' | grep -o -A1 foo
foo
--          # GNU prints this, uutils didn't
foo

The context options are now kept with -o, and Writer::write_line skips non-matching (context) lines when -o is set, so the group tracking in session_write_match_with_context works as it does without -o. Output with -o still never includes context lines, which was the point of #67/#72.

Checked against GNU grep 3.11 on my machine: -A1, -B1, -C1 -n (adjacent groups, no separator), -A0, --group-separator=XX, --no-group-separator, -b, -m1, -c, -l, -v -o -A0, --color=always and -T -n all match. The one remaining difference, the separator between files with -A0 f1 f2, also happens without -o and is what #131 is about.

Tests:

  • New only_matching_with_context_keeps_group_separator; it fails on main. The existing only_matching_with_context still passes.
  • cargo test, cargo fmt --all -- --check and cargo clippy --all-targets --workspace -- -D warnings pass locally.

I used an AI assistant (Grok Bot) to help write this change. I reviewed the diff, compared the output with GNU grep 3.11 and ran the tests above.

GNU grep does not print context lines under -o, but it still tracks the
context groups and prints the group separator between groups that are not
adjacent. Keep the context settings with -o and skip only the context
lines themselves when writing.

Fixes uutils#175
Copilot AI balanced review requested due to automatic review settings November 9, 2026 16:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@codspeed

codspeed Bot commented Nov 9, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 3 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing sarankumar1325:grep-only-matching-group-separator (1812619) with main (322d1db)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grep - -o with -A/-B/-C drops the -- group separators GNU prints

2 participants