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., 675 Mass Ave, Cambridge, MA 02139, 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 #define min(x, y) ((x) < (y) ? (x) : (y))
35 static char *find_field ();
36 static int different ();
37 static void check_file ();
39 static void writeline ();
41 /* The name this program was run with. */
44 /* Number of fields to skip on each line when doing comparisons. */
45 static int skip_fields;
47 /* Number of chars to skip after skipping any fields. */
48 static int skip_chars;
50 /* Number of chars to compare; if 0, compare the whole lines. */
51 static int check_chars;
55 count_occurrences, /* -c Print count before output lines. */
56 count_none /* Default. Do not print counts. */
59 /* Whether and how to precede the output lines with a count of the number of
60 times they occurred in the input. */
61 static enum countmode countmode;
65 output_repeated, /* -d Only lines that are repeated. */
66 output_unique, /* -u Only lines that are not repeated. */
67 output_all /* Default. Print first copy of each line. */
70 /* Which lines to output. */
71 static enum output_mode mode;
73 /* If non-zero, display usage information and exit. */
76 /* If non-zero, print the version on standard output then exit. */
77 static int show_version;
79 static struct option const longopts[] =
81 {"count", no_argument, NULL, 'c'},
82 {"repeated", no_argument, NULL, 'd'},
83 {"unique", no_argument, NULL, 'u'},
84 {"skip-fields", required_argument, NULL, 'f'},
85 {"skip-chars", required_argument, NULL, 's'},
86 {"check-chars", required_argument, NULL, 'w'},
87 {"help", no_argument, &show_help, 1},
88 {"version", no_argument, &show_version, 1},
98 char *infile = "-", *outfile = "-";
100 program_name = argv[0];
105 countmode = count_none;
107 while ((optc = getopt_long (argc, argv, "0123456789cdf:s:uw:", longopts,
125 skip_fields = skip_fields * 10 + optc - '0';
129 countmode = count_occurrences;
133 mode = output_repeated;
136 case 'f': /* Like '-#'. */
137 skip_fields = atoi (optarg);
140 case 's': /* Like '+#'. */
141 skip_chars = atoi (optarg);
145 mode = output_unique;
149 check_chars = atoi (optarg);
159 printf ("uniq - %s\n", version_string);
166 if (optind >= 2 && strcmp (argv[optind - 1], "--") != 0)
168 /* Interpret non-option arguments with leading `+' only
169 if we haven't seen `--'. */
170 while (optind < argc && argv[optind][0] == '+')
171 skip_chars = atoi (argv[optind++]);
175 infile = argv[optind++];
178 outfile = argv[optind++];
181 usage (1); /* Extra arguments. */
183 check_file (infile, outfile);
188 /* Process input file INFILE with output to OUTFILE.
189 If either is "-", use the standard I/O stream for it instead. */
192 check_file (infile, outfile)
193 char *infile, *outfile;
197 struct linebuffer lb1, lb2;
198 struct linebuffer *thisline, *prevline, *exch;
199 char *prevfield, *thisfield;
200 int prevlen, thislen;
203 if (!strcmp (infile, "-"))
206 istream = fopen (infile, "r");
208 error (1, errno, "%s", infile);
210 if (!strcmp (outfile, "-"))
213 ostream = fopen (outfile, "w");
215 error (1, errno, "%s", outfile);
220 initbuffer (thisline);
221 initbuffer (prevline);
223 if (readline (prevline, istream) == 0)
225 prevfield = find_field (prevline);
226 prevlen = prevline->length - (prevfield - prevline->buffer);
228 while (!feof (istream))
230 if (readline (thisline, istream) == 0)
232 thisfield = find_field (thisline);
233 thislen = thisline->length - (thisfield - thisline->buffer);
234 if (!different (thisfield, prevfield, thislen, prevlen))
238 writeline (prevline, ostream, match_count);
244 prevfield = thisfield;
249 writeline (prevline, ostream, match_count);
252 if (ferror (istream) || fclose (istream) == EOF)
253 error (1, errno, "error reading %s", infile);
255 if (ferror (ostream) || fclose (ostream) == EOF)
256 error (1, errno, "error writing %s", outfile);
262 /* Given a linebuffer LINE,
263 return a pointer to the beginning of the line's field to be compared. */
267 struct linebuffer *line;
270 register char *lp = line->buffer;
271 register int size = line->length;
274 for (count = 0; count < skip_fields && i < size; count++)
276 while (i < size && ISBLANK (lp[i]))
278 while (i < size && !ISBLANK (lp[i]))
282 for (count = 0; count < skip_chars && i < size; count++)
288 /* Return zero if two strings OLD and NEW match, nonzero if not.
289 OLD and NEW point not to the beginnings of the lines
290 but rather to the beginnings of the fields to compare.
291 OLDLEN and NEWLEN are their lengths. */
294 different (old, new, oldlen, newlen)
304 if (oldlen > check_chars)
305 oldlen = check_chars;
306 if (newlen > check_chars)
307 newlen = check_chars;
309 order = memcmp (old, new, min (oldlen, newlen));
311 return oldlen - newlen;
315 /* Output the line in linebuffer LINE to stream STREAM
316 provided that the switches say it should be output.
317 If requested, print the number of times it occurred, as well;
318 LINECOUNT + 1 is the number of times that the line occurred. */
321 writeline (line, stream, linecount)
322 struct linebuffer *line;
326 if ((mode == output_unique && linecount != 0)
327 || (mode == output_repeated && linecount == 0))
330 if (countmode == count_occurrences)
331 fprintf (stream, "%7d\t", linecount + 1);
333 fwrite (line->buffer, sizeof (char), line->length, stream);
342 fprintf (stderr, "Try `%s --help' for more information.\n",
347 Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\
351 Discard all but one of successive identical lines from INPUT (or\n\
352 standard input), writing to OUTPUT (or standard output).\n\
354 -c, --count prefix lines by the number of occurrences\n\
355 -d, --repeated only print duplicate lines\n\
356 -f, --skip-fields=N avoid comparing the N first fields\n\
357 -s, --skip-chars=N avoid comparing the N first characters\n\
358 -u, --unique only print unique lines\n\
359 -w, --check-chars=N compare no more then N characters in lines\n\
362 --help display this help and exit\n\
363 --version output version information and exit\n\
365 A field is a run of whitespace, than non-whitespace characters.\n\
366 Fields are skipped before chars. \n\