git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] grep: fix --column --only-match for 2nd and later matches

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
May 4, 2026, 13:10 UTC
Message-ID
<a7819ca3-7e57-4ba1-94e3-aab427fad678@gmail.com>
In-Reply-To
<9bd69678-f04b-41d2-ad74-a386820d34c8@web.de>
Hi René
On 24/04/2026 22:04, René Scharfe wrote:
Show 9 quoted lines
> diff --git a/grep.c b/grep.c
> index c7e1dc1e0e..a54e5d86a9 100644
> --- a/grep.c
> +++ b/grep.c
> @@ -1267,6 +1267,7 @@ static void show_line(struct grep_opt *opt,
>   		regmatch_t match;
>   		enum grep_context ctx = GREP_CONTEXT_BODY;
>   		int eflags = 0;
> +		const char *start = bol;
Here we save a pointer to the start of the line
Show 7 quoted lines
>   		if (want_color(opt->color)) {
>   			if (sign == ':')
> @@ -1285,6 +1286,7 @@ static void show_line(struct grep_opt *opt,
>   			if (match.rm_so == match.rm_eo)
>   				break;
>   
> +			cno = bol - start + match.rm_so + 1;
and then we calculate the column number relative to that.
That looks good, thanks for fixing it
Phillip
Show 30 quoted lines
>   			if (opt->only_matching)
>   				show_line_header(opt, name, lno, cno, sign);
>   			else
> @@ -1294,7 +1296,6 @@ static void show_line(struct grep_opt *opt,
>   			if (opt->only_matching)
>   				opt->output(opt, "\n", 1);
>   			bol += match.rm_eo;
> -			cno += match.rm_eo;
>   			rest -= match.rm_eo;
>   			eflags = REG_NOTBOL;
>   		}
> diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
> index 64ac4f04ee..bd439563d6 100755
> --- a/t/t7810-grep.sh
> +++ b/t/t7810-grep.sh
> @@ -322,11 +322,11 @@ do
>   		${HC}file:1:5:mmap
>   		${HC}file:2:5:mmap
>   		${HC}file:3:5:mmap
> -		${HC}file:3:13:mmap
> +		${HC}file:3:14:mmap
>   		${HC}file:4:5:mmap
> -		${HC}file:4:13:mmap
> +		${HC}file:4:14:mmap
>   		${HC}file:5:5:mmap
> -		${HC}file:5:13:mmap
> +		${HC}file:5:14:mmap
>   		EOF
>   		git grep --column -n -o -e mmap $H >actual &&
>   		test_cmp expected actual
Previous: René Scharfe
Message 5 of 5 in “git grep bug with --column and --only-matching”
  1. Brandon ChinnApr 21, 2026
  2. René ScharfeApr 21, 2026
  3. Phillip WoodApr 23, 2026
  4. grep: fix --column --only-match for 2nd and later matchesRené Scharfe, Apr 24, 2026
  5. Phillip WoodMay 4, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.