Teach comm(1) and uniq(1) about an option for case-insensitive work.

PR:		3042
Submitted by:	graphix@iastate.edu (Kent Vander Velden)
This commit is contained in:
Joerg Wunsch
1997-09-07 15:09:22 +00:00
parent c816cfc962
commit 2ca7dc1598
4 changed files with 42 additions and 12 deletions
+5 -2
View File
@@ -32,7 +32,8 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" @(#)comm.1 8.1 (Berkeley) 6/6/93 .\" From: @(#)comm.1 8.1 (Berkeley) 6/6/93
.\" $Id$
.\" .\"
.Dd June 6, 1993 .Dd June 6, 1993
.Os .Os
@@ -42,7 +43,7 @@
.Nd select or reject lines common to two files .Nd select or reject lines common to two files
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm comm .Nm comm
.Op Fl 123 .Op Fl 123i
.Ar file1 file2 .Ar file1 file2
.Sh DESCRIPTION .Sh DESCRIPTION
The The
@@ -69,6 +70,8 @@ Suppress printing of column 1.
Suppress printing of column 2. Suppress printing of column 2.
.It Fl 3 .It Fl 3
Suppress printing of column 3. Suppress printing of column 3.
.It Fl i
Case insensitive comparison of lines.
.El .El
.Pp .Pp
Each column will have a number of tab characters prepended to it Each column will have a number of tab characters prepended to it
+19 -5
View File
@@ -41,7 +41,11 @@ static char copyright[] =
#endif /* not lint */ #endif /* not lint */
#ifndef lint #ifndef lint
static char sccsid[] = "@(#)comm.c 8.4 (Berkeley) 5/4/95"; #if 0
static char sccsid[] = "From: @(#)comm.c 8.4 (Berkeley) 5/4/95";
#endif
static const char rcsid[] =
"$Id$";
#endif /* not lint */ #endif /* not lint */
#include <err.h> #include <err.h>
@@ -66,13 +70,15 @@ main(argc, argv)
char *argv[]; char *argv[];
{ {
int comp, file1done, file2done, read1, read2; int comp, file1done, file2done, read1, read2;
int ch, flag1, flag2, flag3; int ch, flag1, flag2, flag3, iflag;
FILE *fp1, *fp2; FILE *fp1, *fp2;
char *col1, *col2, *col3; char *col1, *col2, *col3;
char **p, line1[MAXLINELEN], line2[MAXLINELEN]; char **p, line1[MAXLINELEN], line2[MAXLINELEN];
flag1 = flag2 = flag3 = 1; flag1 = flag2 = flag3 = 1;
while ((ch = getopt(argc, argv, "-123")) != -1) iflag = 0;
while ((ch = getopt(argc, argv, "-123i")) != -1)
switch(ch) { switch(ch) {
case '-': case '-':
--optind; --optind;
@@ -86,6 +92,9 @@ main(argc, argv)
case '3': case '3':
flag3 = 0; flag3 = 0;
break; break;
case 'i':
iflag = 1;
break;
case '?': case '?':
default: default:
usage(); usage();
@@ -129,7 +138,12 @@ done: argc -= optind;
} }
/* lines are the same */ /* lines are the same */
if (!(comp = strcmp(line1, line2))) { if(iflag)
comp = strcasecmp(line1, line2);
else
comp = strcmp(line1, line2);
if (!comp) {
read1 = read2 = 1; read1 = read2 = 1;
if (col3) if (col3)
(void)printf("%s%s", col3, line1); (void)printf("%s%s", col3, line1);
@@ -180,6 +194,6 @@ file(name)
static void static void
usage() usage()
{ {
(void)fprintf(stderr, "usage: comm [-123] file1 file2\n"); (void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
exit(1); exit(1);
} }
+5 -1
View File
@@ -32,7 +32,8 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" @(#)uniq.1 8.1 (Berkeley) 6/6/93 .\" From: @(#)uniq.1 8.1 (Berkeley) 6/6/93
.\" $Id$
.\" .\"
.Dd June 6, 1993 .Dd June 6, 1993
.Dt UNIQ 1 .Dt UNIQ 1
@@ -43,6 +44,7 @@
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl c | Fl d | Fl u .Op Fl c | Fl d | Fl u
.Op Fl i
.Op Fl f Ar fields .Op Fl f Ar fields
.Op Fl s Ar chars .Op Fl s Ar chars
.Oo .Oo
@@ -87,6 +89,8 @@ fields will be ignored.
Character numbers are one based, i.e. the first character is character one. Character numbers are one based, i.e. the first character is character one.
.It Fl u .It Fl u
Don't output lines that are repeated in the input. Don't output lines that are repeated in the input.
.It Fl i
Case insensitive comparison of lines.
.\".It Fl Ns Ar n .\".It Fl Ns Ar n
.\"(Deprecated; replaced by .\"(Deprecated; replaced by
.\".Fl f ) . .\".Fl f ) .
+13 -4
View File
@@ -45,7 +45,7 @@ static const char copyright[] =
static char sccsid[] = "@(#)uniq.c 8.3 (Berkeley) 5/4/95"; static char sccsid[] = "@(#)uniq.c 8.3 (Berkeley) 5/4/95";
#endif #endif
static const char rcsid[] = static const char rcsid[] =
"$Id$"; "$Id: uniq.c,v 1.3 1997/08/21 06:51:10 charnier Exp $";
#endif /* not lint */ #endif /* not lint */
#include <ctype.h> #include <ctype.h>
@@ -75,9 +75,10 @@ main (argc, argv)
FILE *ifp, *ofp; FILE *ifp, *ofp;
int ch; int ch;
char *prevline, *thisline, *p; char *prevline, *thisline, *p;
int iflag = 0, comp;
obsolete(argv); obsolete(argv);
while ((ch = getopt(argc, argv, "-cdf:s:u")) != -1) while ((ch = getopt(argc, argv, "-cdif:s:u")) != -1)
switch (ch) { switch (ch) {
case '-': case '-':
--optind; --optind;
@@ -88,6 +89,9 @@ main (argc, argv)
case 'd': case 'd':
dflag = 1; dflag = 1;
break; break;
case 'i':
iflag = 1;
break;
case 'f': case 'f':
numfields = strtol(optarg, &p, 10); numfields = strtol(optarg, &p, 10);
if (numfields < 0 || *p) if (numfields < 0 || *p)
@@ -152,7 +156,12 @@ done: argc -= optind;
} }
/* If different, print; set previous to new value. */ /* If different, print; set previous to new value. */
if (strcmp(t1, t2)) { if (iflag)
comp = strcasecmp(t1, t2);
else
comp = strcmp(t1, t2);
if (comp) {
show(ofp, prevline); show(ofp, prevline);
t1 = prevline; t1 = prevline;
prevline = thisline; prevline = thisline;
@@ -245,6 +254,6 @@ static void
usage() usage()
{ {
(void)fprintf(stderr, (void)fprintf(stderr,
"usage: uniq [-c | -du] [-f fields] [-s chars] [input [output]]\n"); "usage: uniq [-c | -du | -i] [-f fields] [-s chars] [input [output]]\n");
exit(1); exit(1);
} }