diff options
author | Bernhard Rosenkraenzer <bero@arklinux.org> | 2001-11-19 13:23:29 +0000 |
---|---|---|
committer | Bernhard Rosenkraenzer <bero@arklinux.org> | 2001-11-19 13:23:29 +0000 |
commit | f6e68a1c2efaec7d4e7474cd06c2638f8779fe74 (patch) | |
tree | 10fb7422e1c5ad7bd81d2d6b0b47ecb2c67b05b5 | |
parent | f7ceb1457ce775d561f60ba5964f96e1db3d3ac1 (diff) | |
download | grep-f6e68a1c2efaec7d4e7474cd06c2638f8779fe74.tar.gz |
Add -o option
* src/grep.c: Add --only-matching (-o) switch (see NEWS)
* doc/grep.texi, doc/grep.1, NEWS: Document changes
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | NEWS | 4 | ||||
-rw-r--r-- | doc/grep.1 | 6 | ||||
-rw-r--r-- | doc/grep.texi | 7 | ||||
-rw-r--r-- | po/grep.pot | 2 | ||||
-rw-r--r-- | src/grep.c | 35 | ||||
-rwxr-xr-x | tests/backref.sh | 2 | ||||
-rw-r--r-- | tests/bre.sh | 2 | ||||
-rw-r--r-- | tests/empty.sh | 2 | ||||
-rw-r--r-- | tests/ere.sh | 2 | ||||
-rwxr-xr-x | tests/file.sh | 2 | ||||
-rw-r--r-- | tests/khadafy.sh | 2 | ||||
-rw-r--r-- | tests/options.sh | 2 | ||||
-rw-r--r-- | tests/spencer1.sh | 2 | ||||
-rw-r--r-- | tests/spencer2.sh | 2 | ||||
-rw-r--r-- | tests/status.sh | 2 | ||||
-rw-r--r-- | tests/warning.sh | 2 |
17 files changed, 66 insertions, 14 deletions
@@ -1,3 +1,7 @@ +2001-11-19 Bernhard Rosenkraenzer <bero@redhat.com> + * src/grep.c: Add --only-matching (-o) switch (see NEWS) + * doc/grep.texi, doc/grep.1, NEWS: Document changes + 2001-09-25 Bernhard Rosenkraenzer <bero@redhat.com> * configure.in: Detect pcre correctly even when it's in non-standard locations, using pcre-config @@ -1,4 +1,8 @@ Version 2.5 + - The new option --only-matching (-o) will print only the part of matching + lines that matches the pattern. This is useful, for example, to extract + IP addresses from log files. + - i18n bug fixed ([A-Z0-9] wouldn't match A in locales other than C on systems using recent glibc builds @@ -12,7 +12,7 @@ .de Id .ds Dt \\$4 .. -.Id $Id: grep.1,v 1.21 2001/09/25 10:00:16 bero Exp $ +.Id $Id: grep.1,v 1.22 2001/11/19 13:23:29 bero Exp $ .TH GREP 1 \*(Dt "GNU Project" .SH NAME grep, egrep, fgrep \- print lines matching a pattern @@ -292,6 +292,10 @@ is operating, or if an I/O error occurs. Prefix each line of output with the line number within its input file. .TP +.BR \-o ", " \-\^\-only-matching +Show only the part of a matching line that matches +.I PATTERN. +.TP .BR \-\^\-line-buffering Use line buffering, it can be a performance penality. .TP diff --git a/doc/grep.texi b/doc/grep.texi index 70021cc3..4b39a94a 100644 --- a/doc/grep.texi +++ b/doc/grep.texi @@ -185,6 +185,13 @@ The scanning of every file will stop on the first match. @cindex line numbering Prefix each line of output with the line number within its input file. +@item -o +@itemx --only-matching +@opindex -o +@opindex --only-matching +@cindex only matching +Print only the part of matching lines that actually matches @var{pattern}. + @item -q @itemx --quiet @itemx --silent diff --git a/po/grep.pot b/po/grep.pot index aec90e45..7298c5b3 100644 --- a/po/grep.pot +++ b/po/grep.pot @@ -6,7 +6,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" -"POT-Creation-Date: 2001-09-26 18:24+0200\n" +"POT-Creation-Date: 2001-09-26 18:44+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <LL@li.org>\n" @@ -69,6 +69,9 @@ static int mmap_option; /* If nonzero, use grep_color marker. */ static int color_option; +/* If nonzero, show only the part of a line matching the expression. */ +static int only_matching; + /* The color string used. The user can overwrite it using the environment variable GREP_COLOR. The default is to print red. */ static const char *grep_color = "01;31"; @@ -77,7 +80,7 @@ static struct exclude *excluded_patterns; static struct exclude *included_patterns; /* Short options. */ static char const short_options[] = -"0123456789A:B:C:D:EFGHIPUVX:abcd:e:f:hiKLlm:nqRrsuvwxyZz"; +"0123456789A:B:C:D:EFGHIPUVX:abcd:e:f:hiKLlm:noqRrsuvwxyZz"; /* Non-boolean long options that have no corresponding short equivalents. */ enum @@ -124,6 +127,7 @@ static struct option const long_options[] = {"no-messages", no_argument, NULL, 's'}, {"null", no_argument, NULL, 'Z'}, {"null-data", no_argument, NULL, 'z'}, + {"only-matching", no_argument, NULL, 'o'}, {"perl-regexp", no_argument, NULL, 'P'}, {"quiet", no_argument, NULL, 'q'}, {"recursive", no_argument, NULL, 'r'}, @@ -518,6 +522,28 @@ prline (char const *beg, char const *lim, int sep) #endif print_offset_sep (pos, sep); } + if (only_matching) + { + size_t match_size; + size_t match_offset; + while ((match_offset = (*execute) (beg, lim - beg, &match_size, 1)) + != (size_t) -1) + { + char const *b = beg + match_offset; + if (b == lim) + break; + if(color_option) + printf("\33[%sm", grep_color); + fwrite(b, sizeof (char), match_size, stdout); + if(color_option) + fputs("\33[00m", stdout); + fputs("\n", stdout); + beg = b + match_size; + } + if(line_buffered) + fflush(stdout); + return; + } if (color_option) { size_t match_size; @@ -1021,6 +1047,7 @@ Output control:\n\ --line-buffered flush output on every line\n\ -H, --with-filename print the filename for each match\n\ -h, --no-filename suppress the prefixing filename on output\n\ + -o, --only-matching show only the part of a line matching PATTERN\n\ -q, --quiet, --silent suppress all normal output\n\ --binary-files=TYPE assume that binary files are TYPE\n\ TYPE is 'binary', 'text', or 'without-match'\n\ @@ -1263,6 +1290,8 @@ main (int argc, char **argv) out_after = out_before = -1; /* Default before/after context: chaged by -C/-NUM options */ default_context = 0; + /* Changed by -o option */ + only_matching = 0; /* Internationalization. */ #if defined(HAVE_SETLOCALE) @@ -1443,6 +1472,10 @@ main (int argc, char **argv) out_line = 1; break; + case 'o': + only_matching = 1; + break; + case 'q': exit_on_match = 1; break; diff --git a/tests/backref.sh b/tests/backref.sh index b87ac853..83f6dcc9 100755 --- a/tests/backref.sh +++ b/tests/backref.sh @@ -1,4 +1,4 @@ -#! /bin/sh +#!/bin/sh # Test that backrefs are local to regex. # # diff --git a/tests/bre.sh b/tests/bre.sh index eecdd3ee..e7ccf0a0 100644 --- a/tests/bre.sh +++ b/tests/bre.sh @@ -1,4 +1,4 @@ -#! /bin/sh +#!/bin/sh # Regression test for GNU grep. : ${srcdir=.} diff --git a/tests/empty.sh b/tests/empty.sh index cae640f9..d4225fec 100644 --- a/tests/empty.sh +++ b/tests/empty.sh @@ -1,4 +1,4 @@ -#! /bin/sh +#!/bin/sh # test that the empty file means no pattern # and an empty pattern means match all. diff --git a/tests/ere.sh b/tests/ere.sh index 5bf16952..d57e2fae 100644 --- a/tests/ere.sh +++ b/tests/ere.sh @@ -1,4 +1,4 @@ -#! /bin/sh +#!/bin/sh # Regression test for GNU grep. : ${srcdir=.} diff --git a/tests/file.sh b/tests/file.sh index c3979790..3db95bed 100755 --- a/tests/file.sh +++ b/tests/file.sh @@ -1,4 +1,4 @@ -#! /bin/sh +#!/bin/sh # Test for POSIX.2 options for grep # # grep -E -f pattern_file file diff --git a/tests/khadafy.sh b/tests/khadafy.sh index 141b3d81..e3d2cde4 100644 --- a/tests/khadafy.sh +++ b/tests/khadafy.sh @@ -1,4 +1,4 @@ -#! /bin/sh +#!/bin/sh # Regression test for GNU grep. : ${srcdir=.} diff --git a/tests/options.sh b/tests/options.sh index 34150185..0f185297 100644 --- a/tests/options.sh +++ b/tests/options.sh @@ -1,4 +1,4 @@ -#! /bin/sh +#!/bin/sh # Test for POSIX.2 options for grep # # grep [ -E| -F][ -c| -l| -q ][-insvx] -e pattern_list diff --git a/tests/spencer1.sh b/tests/spencer1.sh index f09b3fa9..2391761a 100644 --- a/tests/spencer1.sh +++ b/tests/spencer1.sh @@ -1,4 +1,4 @@ -#! /bin/sh +#!/bin/sh # Regression test for GNU grep. : ${srcdir=.} diff --git a/tests/spencer2.sh b/tests/spencer2.sh index 19caa8de..a26869c5 100644 --- a/tests/spencer2.sh +++ b/tests/spencer2.sh @@ -1,4 +1,4 @@ -#! /bin/sh +#!/bin/sh # Regression test for GNU grep. : ${srcdir=.} diff --git a/tests/status.sh b/tests/status.sh index 4f08b8a3..94f59294 100644 --- a/tests/status.sh +++ b/tests/status.sh @@ -1,4 +1,4 @@ -#! /bin/sh +#!/bin/sh # Test for status code for GNU grep. # status code # 0 match found diff --git a/tests/warning.sh b/tests/warning.sh index d2dc6d5a..bfca533a 100644 --- a/tests/warning.sh +++ b/tests/warning.sh @@ -1,4 +1,4 @@ -#! /bin/sh +#!/bin/sh # # Tell them not to be alarmed. |