1 /* uniq -- remove duplicate lines from a sorted file
2 Copyright (C) 1986, 1991, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Richard Stallman and David MacKenzie. */
22 /* Get isblank from GNU libc. */
27 #include <sys/types.h>
29 #include "linebuffer.h"
33 /* Undefine, to avoid warning about redefinition on some systems. */
35 #define min(x, y) ((x) < (y) ? (x) : (y))
37 static char *find_field ();
38 static int different ();
39 static void check_file ();
41 static void writeline ();
43 /* The name this program was run with. */
46 /* Number of fields to skip on each line when doing comparisons. */
47 static int skip_fields
;
49 /* Number of chars to skip after skipping any fields. */
50 static int skip_chars
;
52 /* Number of chars to compare; if 0, compare the whole lines. */
53 static int check_chars
;
57 count_occurrences
, /* -c Print count before output lines. */
58 count_none
/* Default. Do not print counts. */
61 /* Whether and how to precede the output lines with a count of the number of
62 times they occurred in the input. */
63 static enum countmode countmode
;
67 output_repeated
, /* -d Only lines that are repeated. */
68 output_unique
, /* -u Only lines that are not repeated. */
69 output_all
/* Default. Print first copy of each line. */
72 /* Which lines to output. */
73 static enum output_mode mode
;
75 /* If non-zero, display usage information and exit. */
78 /* If non-zero, print the version on standard output then exit. */
79 static int show_version
;
81 static struct option
const longopts
[] =
83 {"count", no_argument
, NULL
, 'c'},
84 {"repeated", no_argument
, NULL
, 'd'},
85 {"unique", no_argument
, NULL
, 'u'},
86 {"skip-fields", required_argument
, NULL
, 'f'},
87 {"skip-chars", required_argument
, NULL
, 's'},
88 {"check-chars", required_argument
, NULL
, 'w'},
89 {"help", no_argument
, &show_help
, 1},
90 {"version", no_argument
, &show_version
, 1},
100 char *infile
= "-", *outfile
= "-";
102 program_name
= argv
[0];
107 countmode
= count_none
;
109 while ((optc
= getopt_long (argc
, argv
, "0123456789cdf:s:uw:", longopts
,
127 skip_fields
= skip_fields
* 10 + optc
- '0';
131 countmode
= count_occurrences
;
135 mode
= output_repeated
;
138 case 'f': /* Like '-#'. */
139 skip_fields
= atoi (optarg
);
142 case 's': /* Like '+#'. */
143 skip_chars
= atoi (optarg
);
147 mode
= output_unique
;
151 check_chars
= atoi (optarg
);
161 printf ("uniq - %s\n", version_string
);
168 if (optind
>= 2 && strcmp (argv
[optind
- 1], "--") != 0)
170 /* Interpret non-option arguments with leading `+' only
171 if we haven't seen `--'. */
172 while (optind
< argc
&& argv
[optind
][0] == '+')
173 skip_chars
= atoi (argv
[optind
++]);
177 infile
= argv
[optind
++];
180 outfile
= argv
[optind
++];
183 usage (1); /* Extra arguments. */
185 check_file (infile
, outfile
);
190 /* Process input file INFILE with output to OUTFILE.
191 If either is "-", use the standard I/O stream for it instead. */
194 check_file (infile
, outfile
)
195 char *infile
, *outfile
;
199 struct linebuffer lb1
, lb2
;
200 struct linebuffer
*thisline
, *prevline
, *exch
;
201 char *prevfield
, *thisfield
;
202 int prevlen
, thislen
;
205 if (!strcmp (infile
, "-"))
208 istream
= fopen (infile
, "r");
210 error (1, errno
, "%s", infile
);
212 if (!strcmp (outfile
, "-"))
215 ostream
= fopen (outfile
, "w");
217 error (1, errno
, "%s", outfile
);
222 initbuffer (thisline
);
223 initbuffer (prevline
);
225 if (readline (prevline
, istream
) == 0)
227 prevfield
= find_field (prevline
);
228 prevlen
= prevline
->length
- (prevfield
- prevline
->buffer
);
230 while (!feof (istream
))
232 if (readline (thisline
, istream
) == 0)
234 thisfield
= find_field (thisline
);
235 thislen
= thisline
->length
- (thisfield
- thisline
->buffer
);
236 if (!different (thisfield
, prevfield
, thislen
, prevlen
))
240 writeline (prevline
, ostream
, match_count
);
246 prevfield
= thisfield
;
251 writeline (prevline
, ostream
, match_count
);
254 if (ferror (istream
) || fclose (istream
) == EOF
)
255 error (1, errno
, "error reading %s", infile
);
257 if (ferror (ostream
) || fclose (ostream
) == EOF
)
258 error (1, errno
, "error writing %s", outfile
);
264 /* Given a linebuffer LINE,
265 return a pointer to the beginning of the line's field to be compared. */
269 struct linebuffer
*line
;
272 register char *lp
= line
->buffer
;
273 register int size
= line
->length
;
276 for (count
= 0; count
< skip_fields
&& i
< size
; count
++)
278 while (i
< size
&& ISBLANK (lp
[i
]))
280 while (i
< size
&& !ISBLANK (lp
[i
]))
284 for (count
= 0; count
< skip_chars
&& i
< size
; count
++)
290 /* Return zero if two strings OLD and NEW match, nonzero if not.
291 OLD and NEW point not to the beginnings of the lines
292 but rather to the beginnings of the fields to compare.
293 OLDLEN and NEWLEN are their lengths. */
296 different (old
, new, oldlen
, newlen
)
306 if (oldlen
> check_chars
)
307 oldlen
= check_chars
;
308 if (newlen
> check_chars
)
309 newlen
= check_chars
;
311 order
= memcmp (old
, new, min (oldlen
, newlen
));
313 return oldlen
- newlen
;
317 /* Output the line in linebuffer LINE to stream STREAM
318 provided that the switches say it should be output.
319 If requested, print the number of times it occurred, as well;
320 LINECOUNT + 1 is the number of times that the line occurred. */
323 writeline (line
, stream
, linecount
)
324 struct linebuffer
*line
;
328 if ((mode
== output_unique
&& linecount
!= 0)
329 || (mode
== output_repeated
&& linecount
== 0))
332 if (countmode
== count_occurrences
)
333 fprintf (stream
, "%7d\t", linecount
+ 1);
335 fwrite (line
->buffer
, sizeof (char), line
->length
, stream
);
344 fprintf (stderr
, "Try `%s --help' for more information.\n",
349 Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\
353 Discard all but one of successive identical lines from INPUT (or\n\
354 standard input), writing to OUTPUT (or standard output).\n\
356 -c, --count prefix lines by the number of occurrences\n\
357 -d, --repeated only print duplicate lines\n\
358 -f, --skip-fields=N avoid comparing the N first fields\n\
359 -s, --skip-chars=N avoid comparing the N first characters\n\
360 -u, --unique only print unique lines\n\
361 -w, --check-chars=N compare no more than N characters in lines\n\
364 --help display this help and exit\n\
365 --version output version information and exit\n\
367 A field is a run of whitespace, than non-whitespace characters.\n\
368 Fields are skipped before chars. \n\