Implement a -H flag for grep, useful for combining e.g. find and grep.
Can anyone commit it?
Index: grep.1
===================================================================
RCS file: /cvs/src/usr.bin/grep/grep.1,v
retrieving revision 1.39
diff -u -r1.39 grep.1
--- grep.1 3 Sep 2010 11:09:28 -0000 1.39
+++ grep.1 21 Feb 2011 12:29:41 -0000
@@ -38,7 +38,7 @@
.Sh SYNOPSIS
.Nm grep
.Bk -words
-.Op Fl abcEFGhIiLlnqRsUVvwxZ
+.Op Fl abcEFGHhIiLlnqRsUVvwxZ
.Op Fl A Ar num
.Op Fl B Ar num
.Op Fl C Ns Op Ar num
@@ -184,6 +184,10 @@
.Nm grep
to behave as traditional
.Nm grep ) .
+.It Fl H
+Always print filename headers
+.Pq i.e. filenames
+with output lines.
.It Fl h
Never print filename headers
.Pq i.e. filenames
@@ -349,7 +353,7 @@
specification.
.Pp
The flags
-.Op Fl AaBbCGhILRUVwZ
+.Op Fl AaBbCGHhILRUVwZ
are extensions to that specification, and the behaviour of the
.Fl f
flag when used with an empty pattern file is left undefined.
Index: grep.c
===================================================================
RCS file: /cvs/src/usr.bin/grep/grep.c,v
retrieving revision 1.42
diff -u -r1.42 grep.c
--- grep.c 2 Jul 2010 22:18:03 -0000 1.42
+++ grep.c 21 Feb 2011 12:29:41 -0000
@@ -62,6 +62,7 @@
int Eflag; /* -E: interpret pattern as extended regexp */
int Fflag; /* -F: interpret pattern as list of fixed strings */
int Gflag; /* -G: interpret pattern as basic regexp */
+int Hflag; /* -H: always print filename header */
int Lflag; /* -L: only show names of files with no matches */
int Rflag; /* -R: recursively search directory trees */
#ifndef NOZ
@@ -106,9 +107,9 @@
{
fprintf(stderr,
#ifdef NOZ
- "usage: %s [-abcEFGhIiLlnqRsUVvwx] [-A num] [-B num] [-C[num]]\n"
+ "usage: %s [-abcEFGHhIiLlnqRsUVvwx] [-A num] [-B num] [-C[num]]\n"
#else
- "usage: %s [-abcEFGhIiLlnqRsUVvwxZ] [-A num] [-B num] [-C[num]]\n"
+ "usage: %s [-abcEFGHhIiLlnqRsUVvwxZ] [-A num] [-B num] [-C[num]]\n"
#endif
"\t[-e pattern] [-f file] [--binary-files=value]
[--context[=num]]\n"
"\t[--line-buffered] [pattern] [file ...]\n", __progname);
@@ -116,9 +117,9 @@
}
#ifdef NOZ
-static char *optstr = "0123456789A:B:CEFGILRUVabce:f:hilnqrsuvwxy";
+static char *optstr = "0123456789A:B:CEFGHILRUVabce:f:hilnqrsuvwxy";
#else
-static char *optstr = "0123456789A:B:CEFGILRUVZabce:f:hilnqrsuvwxy";
+static char *optstr = "0123456789A:B:CEFGHILRUVZabce:f:hilnqrsuvwxy";
#endif
struct option long_options[] =
@@ -134,6 +135,7 @@
{"extended-regexp", no_argument, NULL, 'E'},
{"fixed-strings", no_argument, NULL, 'F'},
{"basic-regexp", no_argument, NULL, 'G'},
+ {"with-filename", no_argument, NULL, 'H'},
{"binary", no_argument, NULL, 'U'},
{"version", no_argument, NULL, 'V'},
{"text", no_argument, NULL, 'a'},
@@ -315,6 +317,9 @@
Eflag = Fflag = 0;
Gflag++;
break;
+ case 'H':
+ Hflag++;
+ break;
case 'I':
binbehave = BIN_FILE_SKIP;
break;
@@ -476,7 +481,7 @@
if (lbflag)
setlinebuf(stdout);
- if ((argc == 0 || argc == 1) && !Rflag)
+ if ((argc == 0 || argc == 1) && !Rflag && !Hflag)
hflag = 1;
if (argc == 0)
Index: grep.h
===================================================================
RCS file: /cvs/src/usr.bin/grep/grep.h,v
retrieving revision 1.15
diff -u -r1.15 grep.h
--- grep.h 5 Apr 2010 03:03:55 -0000 1.15
+++ grep.h 21 Feb 2011 12:29:41 -0000
@@ -63,7 +63,7 @@
extern int cflags, eflags;
/* Command line flags */
-extern int Aflag, Bflag, Eflag, Fflag, Gflag, Lflag,
+extern int Aflag, Bflag, Eflag, Fflag, Gflag, Hflag, Lflag,
Rflag, Zflag,
bflag, cflag, hflag, iflag, lflag, nflag, qflag, sflag,
vflag, wflag, xflag;