1 /* uniq -- remove duplicate lines from a sorted file
2 Copyright (C) 86, 91, 1995-2006 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 Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Richard Stallman and David MacKenzie. */
24 #include <sys/types.h>
28 #include "linebuffer.h"
30 #include "hard-locale.h"
35 #include "memcasecmp.h"
37 /* The official name of this program (e.g., no `g' prefix). */
38 #define PROGRAM_NAME "uniq"
40 #define AUTHORS "Richard Stallman", "David MacKenzie"
42 #define SWAP_LINES(A, B) \
45 struct linebuffer *_tmp; \
52 /* The name this program was run with. */
55 /* True if the LC_COLLATE locale is hard. */
56 static bool hard_LC_COLLATE
;
58 /* Number of fields to skip on each line when doing comparisons. */
59 static size_t skip_fields
;
61 /* Number of chars to skip after skipping any fields. */
62 static size_t skip_chars
;
64 /* Number of chars to compare. */
65 static size_t check_chars
;
69 count_occurrences
, /* -c Print count before output lines. */
70 count_none
/* Default. Do not print counts. */
73 /* Whether and how to precede the output lines with a count of the number of
74 times they occurred in the input. */
75 static enum countmode countmode
;
77 /* Which lines to output: unique lines, the first of a group of
78 repeated lines, and the second and subsequented of a group of
80 static bool output_unique
;
81 static bool output_first_repeated
;
82 static bool output_later_repeated
;
84 /* If true, ignore case when comparing. */
85 static bool ignore_case
;
89 /* No delimiters output. --all-repeated[=none] */
92 /* Delimiter precedes all groups. --all-repeated=prepend */
95 /* Delimit all groups. --all-repeated=separate */
99 static char const *const delimit_method_string
[] =
101 "none", "prepend", "separate", NULL
104 static enum delimit_method
const delimit_method_map
[] =
106 DM_NONE
, DM_PREPEND
, DM_SEPARATE
109 /* Select whether/how to delimit groups of duplicate lines. */
110 static enum delimit_method delimit_groups
;
112 static struct option
const longopts
[] =
114 {"count", no_argument
, NULL
, 'c'},
115 {"repeated", no_argument
, NULL
, 'd'},
116 {"all-repeated", optional_argument
, NULL
, 'D'},
117 {"ignore-case", no_argument
, NULL
, 'i'},
118 {"unique", no_argument
, NULL
, 'u'},
119 {"skip-fields", required_argument
, NULL
, 'f'},
120 {"skip-chars", required_argument
, NULL
, 's'},
121 {"check-chars", required_argument
, NULL
, 'w'},
122 {GETOPT_HELP_OPTION_DECL
},
123 {GETOPT_VERSION_OPTION_DECL
},
130 if (status
!= EXIT_SUCCESS
)
131 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
136 Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\
140 Discard all but one of successive identical lines from INPUT (or\n\
141 standard input), writing to OUTPUT (or standard output).\n\
145 Mandatory arguments to long options are mandatory for short options too.\n\
148 -c, --count prefix lines by the number of occurrences\n\
149 -d, --repeated only print duplicate lines\n\
152 -D, --all-repeated[=delimit-method] print all duplicate lines\n\
153 delimit-method={none(default),prepend,separate}\n\
154 Delimiting is done with blank lines.\n\
155 -f, --skip-fields=N avoid comparing the first N fields\n\
156 -i, --ignore-case ignore differences in case when comparing\n\
157 -s, --skip-chars=N avoid comparing the first N characters\n\
158 -u, --unique only print unique lines\n\
161 -w, --check-chars=N compare no more than N characters in lines\n\
163 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
164 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
167 A field is a run of whitespace, then non-whitespace characters.\n\
168 Fields are skipped before chars.\n\
170 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
175 /* Convert OPT to size_t, reporting an error using MSGID if it does
179 size_opt (char const *opt
, char const *msgid
)
181 unsigned long int size
;
182 if (xstrtoul (opt
, NULL
, 10, &size
, "") != LONGINT_OK
184 error (EXIT_FAILURE
, 0, "%s: %s", opt
, _(msgid
));
188 /* Given a linebuffer LINE,
189 return a pointer to the beginning of the line's field to be compared. */
192 find_field (const struct linebuffer
*line
)
195 char *lp
= line
->buffer
;
196 size_t size
= line
->length
- 1;
199 for (count
= 0; count
< skip_fields
&& i
< size
; count
++)
201 while (i
< size
&& isblank (lp
[i
]))
203 while (i
< size
&& !isblank (lp
[i
]))
207 for (count
= 0; count
< skip_chars
&& i
< size
; count
++)
213 /* Return false if two strings OLD and NEW match, true if not.
214 OLD and NEW point not to the beginnings of the lines
215 but rather to the beginnings of the fields to compare.
216 OLDLEN and NEWLEN are their lengths. */
219 different (char *old
, char *new, size_t oldlen
, size_t newlen
)
221 if (check_chars
< oldlen
)
222 oldlen
= check_chars
;
223 if (check_chars
< newlen
)
224 newlen
= check_chars
;
228 /* FIXME: This should invoke strcoll somehow. */
229 return oldlen
!= newlen
|| memcasecmp (old
, new, oldlen
);
231 else if (hard_LC_COLLATE
)
232 return xmemcoll (old
, oldlen
, new, newlen
) != 0;
234 return oldlen
!= newlen
|| memcmp (old
, new, oldlen
);
237 /* Output the line in linebuffer LINE to standard output
238 provided that the switches say it should be output.
239 MATCH is true if the line matches the previous line.
240 If requested, print the number of times it occurred, as well;
241 LINECOUNT + 1 is the number of times that the line occurred. */
244 writeline (struct linebuffer
const *line
,
245 bool match
, uintmax_t linecount
)
247 if (! (linecount
== 0 ? output_unique
248 : !match
? output_first_repeated
249 : output_later_repeated
))
252 if (countmode
== count_occurrences
)
253 printf ("%7" PRIuMAX
" ", linecount
+ 1);
255 fwrite (line
->buffer
, sizeof (char), line
->length
, stdout
);
258 /* Process input file INFILE with output to OUTFILE.
259 If either is "-", use the standard I/O stream for it instead. */
262 check_file (const char *infile
, const char *outfile
)
264 struct linebuffer lb1
, lb2
;
265 struct linebuffer
*thisline
, *prevline
;
267 if (! (STREQ (infile
, "-") || freopen (infile
, "r", stdin
)))
268 error (EXIT_FAILURE
, errno
, "%s", infile
);
269 if (! (STREQ (outfile
, "-") || freopen (outfile
, "w", stdout
)))
270 error (EXIT_FAILURE
, errno
, "%s", outfile
);
275 initbuffer (thisline
);
276 initbuffer (prevline
);
278 /* The duplication in the following `if' and `else' blocks is an
279 optimization to distinguish the common case (in which none of
280 the following options has been specified: --count, -repeated,
281 --all-repeated, --unique) from the others. In the common case,
282 this optimization lets uniq output each different line right away,
283 without waiting to see if the next one is different. */
285 if (output_unique
&& output_first_repeated
&& countmode
== count_none
)
287 char *prevfield
IF_LINT (= NULL
);
288 size_t prevlen
IF_LINT (= 0);
290 while (!feof (stdin
))
294 if (readlinebuffer (thisline
, stdin
) == 0)
296 thisfield
= find_field (thisline
);
297 thislen
= thisline
->length
- 1 - (thisfield
- thisline
->buffer
);
298 if (prevline
->length
== 0
299 || different (thisfield
, prevfield
, thislen
, prevlen
))
301 fwrite (thisline
->buffer
, sizeof (char),
302 thisline
->length
, stdout
);
304 SWAP_LINES (prevline
, thisline
);
305 prevfield
= thisfield
;
314 uintmax_t match_count
= 0;
315 bool first_delimiter
= true;
317 if (readlinebuffer (prevline
, stdin
) == 0)
319 prevfield
= find_field (prevline
);
320 prevlen
= prevline
->length
- 1 - (prevfield
- prevline
->buffer
);
322 while (!feof (stdin
))
327 if (readlinebuffer (thisline
, stdin
) == 0)
333 thisfield
= find_field (thisline
);
334 thislen
= thisline
->length
- 1 - (thisfield
- thisline
->buffer
);
335 match
= !different (thisfield
, prevfield
, thislen
, prevlen
);
336 match_count
+= match
;
338 if (match_count
== UINTMAX_MAX
)
340 if (count_occurrences
)
341 error (EXIT_FAILURE
, 0, _("too many repeated lines"));
345 if (delimit_groups
!= DM_NONE
)
349 if (match_count
) /* a previous match */
350 first_delimiter
= false; /* Only used when DM_SEPARATE */
352 else if (match_count
== 1)
354 if ((delimit_groups
== DM_PREPEND
)
355 || (delimit_groups
== DM_SEPARATE
356 && !first_delimiter
))
361 if (!match
|| output_later_repeated
)
363 writeline (prevline
, match
, match_count
);
364 SWAP_LINES (prevline
, thisline
);
365 prevfield
= thisfield
;
372 writeline (prevline
, false, match_count
);
376 if (ferror (stdin
) || fclose (stdin
) != 0)
377 error (EXIT_FAILURE
, 0, _("error reading %s"), infile
);
379 /* stdout is handled via the atexit-invoked close_stdout function. */
385 enum Skip_field_option_type
393 main (int argc
, char **argv
)
396 bool posixly_correct
= (getenv ("POSIXLY_CORRECT") != NULL
);
397 enum Skip_field_option_type skip_field_option_type
= SFO_NONE
;
401 file
[0] = file
[1] = "-";
402 initialize_main (&argc
, &argv
);
403 program_name
= argv
[0];
404 setlocale (LC_ALL
, "");
405 bindtextdomain (PACKAGE
, LOCALEDIR
);
406 textdomain (PACKAGE
);
407 hard_LC_COLLATE
= hard_locale (LC_COLLATE
);
409 atexit (close_stdout
);
413 check_chars
= SIZE_MAX
;
414 output_unique
= output_first_repeated
= true;
415 output_later_repeated
= false;
416 countmode
= count_none
;
417 delimit_groups
= DM_NONE
;
421 /* Parse an operand with leading "+" as a file after "--" was
422 seen; or if pedantic and a file was seen; or if not
426 || (posixly_correct
&& nfiles
!= 0)
427 || ((optc
= getopt_long (argc
, argv
,
428 "-0123456789Dcdf:is:uw:", longopts
, NULL
))
435 error (0, 0, _("extra operand %s"), quote (argv
[optind
]));
436 usage (EXIT_FAILURE
);
438 file
[nfiles
++] = argv
[optind
++];
444 unsigned long int size
;
446 && posix2_version () < 200112
447 && xstrtoul (optarg
, NULL
, 10, &size
, "") == LONGINT_OK
450 else if (nfiles
== 2)
452 error (0, 0, _("extra operand %s"), quote (optarg
));
453 usage (EXIT_FAILURE
);
456 file
[nfiles
++] = optarg
;
471 if (skip_field_option_type
== SFO_NEW
)
474 if (!DECIMAL_DIGIT_ACCUMULATE (skip_fields
, optc
- '0', size_t))
475 error (EXIT_FAILURE
, 0, "%s",
476 _("invalid number of fields to skip"));
477 skip_field_option_type
= SFO_OBSOLETE
;
482 countmode
= count_occurrences
;
486 output_unique
= false;
490 output_unique
= false;
491 output_later_repeated
= true;
493 delimit_groups
= DM_NONE
;
495 delimit_groups
= XARGMATCH ("--all-repeated", optarg
,
496 delimit_method_string
,
501 skip_field_option_type
= SFO_NEW
;
502 skip_fields
= size_opt (optarg
,
503 N_("invalid number of fields to skip"));
511 skip_chars
= size_opt (optarg
,
512 N_("invalid number of bytes to skip"));
516 output_first_repeated
= false;
520 check_chars
= size_opt (optarg
,
521 N_("invalid number of bytes to compare"));
524 case_GETOPT_HELP_CHAR
;
526 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
529 usage (EXIT_FAILURE
);
533 if (countmode
== count_occurrences
&& output_later_repeated
)
536 _("printing all duplicated lines and repeat counts is meaningless"));
537 usage (EXIT_FAILURE
);
540 check_file (file
[0], file
[1]);