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

[PATCH/RFC 4/4] grep: provide option to match line number

From
Bert Wesarg <bert.wesarg@googlemail.com>
Date
May 2, 2011, 11:39 UTC
Message-ID
<1028f8b6b8da69b8cb80e39a543b0775bc4d935a.1304321122.git.bert.wesarg@googlemail.com>
In-Reply-To
<f85d783d43bfe7f9e9f23a10e72e8a2a83033cac.1304321122.git.bert.wesarg@googlemail.com>
Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>
---
 Documentation/git-grep.txt |    6 +++++-
 builtin/grep.c             |   10 ++++++++++
 2 files changed, 15 insertions(+), 1 deletions(-)
diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index d7523b3..05deb58 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -21,7 +21,7 @@ SYNOPSIS
 	   [--color[=<when>] | --no-color]
 	   [-A <post-context>] [-B <pre-context>] [-C <context>]
 	   [-f <file>] [-e] <pattern>
-	   [--and|--or|--not|(|)|-e <pattern>...]
+	   [--and|--or|--not|(|)|-e <pattern>|-@ <line>...]
 	   [--cached | --no-index | <tree>...]
 	   [--] [<pathspec>...]
 
@@ -168,6 +168,10 @@ OPTIONS
 	scripts passing user input to grep.  Multiple patterns are
 	combined by 'or'.
 
+-@::
+	The next parameter is a line number. This special pattern matches
+	this line of the input file.
+
 --and::
 --or::
 --not::
diff --git a/builtin/grep.c b/builtin/grep.c
index 10a1f65..8b11c87 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -733,6 +733,14 @@ static int pattern_callback(const struct option *opt, const char *arg,
 	return 0;
 }
 
+static int lno_pattern_callback(const struct option *opt, const char *arg,
+				int unset)
+{
+	struct grep_opt *grep_opt = opt->value;
+	append_grep_pattern(grep_opt, arg, "-@ option", 0, GREP_LNO);
+	return 0;
+}
+
 static int help_callback(const struct option *opt, const char *arg, int unset)
 {
 	return -1;
@@ -816,6 +824,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 			"read patterns from file", file_callback),
 		{ OPTION_CALLBACK, 'e', NULL, &opt, "pattern",
 			"match <pattern>", PARSE_OPT_NONEG, pattern_callback },
+		{ OPTION_CALLBACK, '@', NULL, &opt, "lno",
+			"match <line>", PARSE_OPT_NONEG, lno_pattern_callback },
 		{ OPTION_CALLBACK, 0, "and", &opt, NULL,
 		  "combine patterns specified with -e",
 		  PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback },
-- 
1.7.5.349.gfeb1a
Previous: Bert WesargNext: Thiago Farina
Message 5 of 22 in “grep: support to match by line number”
  1. 0/4 grep: support to match by line numberBert Wesarg, May 2, 2011
  2. 1/4 grep: prepare for re-using the space of the regexp member in struct grep_patBert Wesarg, May 2, 2011
  3. 2/4 grep: pass current line number down to match_one_patternBert Wesarg, May 2, 2011
  4. 3/4 grep: introduce pattern which matches at line numberBert Wesarg, May 2, 2011
  5. 4/4 grep: provide option to match line numberBert Wesarg, May 2, 2011
  6. Thiago FarinaMay 2, 2011
  7. Bert WesargMay 2, 2011
  8. Thiago FarinaMay 2, 2011
  9. Bert WesargMay 2, 2011
  10. Junio C HamanoMay 2, 2011
  11. Thiago FarinaMay 2, 2011
  12. Bert WesargMay 2, 2011
  13. Sverre RabbelierMay 2, 2011
  14. Bert WesargMay 2, 2011
  15. Junio C HamanoMay 2, 2011
  16. Bert WesargMay 2, 2011
  17. Junio C HamanoMay 2, 2011
  18. Junio C HamanoMay 2, 2011
  19. Jakub NarebskiMay 2, 2011
  20. Bert WesargMay 2, 2011
  21. Junio C HamanoMay 2, 2011
  22. Bert WesargMay 2, 2011

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.