Mon 28 May 2012 06:28:37 PM UTC, original submission:
Attached is a patch that adds a new option (-J) to support multiple fixed multiline pattern matching. We couldn't figure out how to search a fixed multiline pattern using grep, so we've added this option. Please let us know if we're missing something. :) This was originally for our research project, but we thought that this option might be also useful to others.
One way to grep a multiline pattern is to use -z option like "grep -GHnz <pattern> <file>" where <pattern> is "line1.line2.line3". Some small problems with this method would be a) -n option becomes meaningless because it would always return line #1, b) basic regex matching (-G option, default) would be slower than fixed string matching (-F option), and c) it would print whole file content instead of only matching lines if there's any match.
So, we've added a new option to extend grep's fast fixed string matching method for multiline string search. Our patch is quite straightforward.
*Usage:
$ ./grep -JHn "`echo -e "/* The color strings used for matched text.\n The user can overwrite them using the deprecated\n environment variable GREP_COLOR or the new GREP_COLORS."`" main.c
main.c:130:/* The color strings used for matched text.
The user can overwrite them using the deprecated
environment variable GREP_COLOR or the new GREP_COLORS. */
Or for multiple fixed multiline strings,
$ ./grep -JHn -f multi.txt main.c
main.c:130:/* The color strings used for matched text.
The user can overwrite them using the deprecated
environment variable GREP_COLOR or the new GREP_COLORS. */
static const char selected_match_color = "01;31"; / bold red */
static const char context_match_color = "01;31"; / bold red */
main.c:136:/* Other colors. Defaults look damn good. */
static const char filename_color = "35"; / magenta */
static const char line_num_color = "32"; / green */
static const char byte_num_color = "32"; / green */
static const char sep_color = "36"; / cyan */
static const char selected_line_color = ""; / default color pair */
static const char context_line_color = ""; / default color pair */
where "multi.txt" has
/* The color strings used for matched text.
The user can overwrite them using the deprecated
environment variable GREP_COLOR or the new GREP_COLORS. */
static const char selected_match_color = "01;31"; / bold red */
static const char context_match_color = "01;31"; / bold red */
^@/* Other colors. Defaults look damn good. */
static const char filename_color = "35"; / magenta */
static const char line_num_color = "32"; / green */
static const char byte_num_color = "32"; / green */
static const char sep_color = "36"; / cyan */
static const char selected_line_color = ""; / default color pair */
static const char context_line_color = ""; / default color pair */
^@ denotes NUL character for separating multiline patterns. In other words, line 1-5 is the 1st multiline pattern that matches with main.c line #130, and line 6-12 is the 2nd multiline pattern that matches with main.c line #136.
Best,
Jiyong Jang & Maverick Woo
|