uniq: Correctly document the -D option.

The -D option takes an optional argument; modify the usage message
and the manual page's synopsis to correctly reflect this.  Also
update the tests to exercise -D with and without an argument.

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	asomers
Differential Revision:	https://reviews.freebsd.org/D47999

(cherry picked from commit b93791f5e7b0246b121dd98c10d6563298d6b2b0)

uniq: Fix off-by-one bug in -cD case.

When printing only duplicated lines, the first line of each set is not
printed until we encounter the second.  When that happens, we need to
increment the repetition count between printing the first and the
second line, so that if we are also printing counts, we don't print the
same (pre-increment) count twice.

MFC after:	1 week
PR:		275764
Reported by:	Yu-Sheng Ma <s110062131@m110.nthu.edu.tw>
Submitted by:	Daniel Tameling <tamelingdaniel@gmail.com> (original patch)
Sponsored by:	Klara, Inc.
Reviewed by:	tamelingdaniel_gmail.com, asomers, emaste
Differential Revision:	https://reviews.freebsd.org/D48000

(cherry picked from commit c3f8900e696998c410dc16f9bd9d45c24c413e6b)
This commit is contained in:
Dag-Erling Smørgrav 2024-12-09 20:44:34 +01:00
parent 259dcedc4a
commit efefeefd43
3 changed files with 40 additions and 6 deletions

View file

@ -53,7 +53,12 @@ count_repeated_head() {
count_repeated_body() {
printf "a\na\nb\nb\na\n" >input
printf " 2 a\n 2 b\n" >expected
atf_check_uniq -cd
atf_check_uniq -c -d
atf_check_uniq -dc
atf_check_uniq -d -c
atf_check_uniq --count --repeated
atf_check_uniq --repeated --count
}
atf_test_case all_repeated
@ -64,7 +69,34 @@ all_repeated_body() {
printf "a\na\nb\na\na\n" >input
printf "a\na\na\na\n" >expected
atf_check_uniq -D
atf_check_uniq -Dnone
atf_check_uniq --all-repeated
atf_check_uniq --all-repeated=none
printf "\na\na\n\na\na\n" >expected
atf_check_uniq -Dprepend
atf_check_uniq --all-repeated=prepend
printf "a\na\n\na\na\n" >expected
atf_check_uniq -Dseparate
atf_check_uniq --all-repeated=separate
}
atf_test_case count_all_repeated
count_all_repeated_head() {
atf_set descr "count and print every instance of repeated lines"
}
count_all_repeated_body() {
printf "a\na\nb\na\na\n" >input
printf " 1 a\n 2 a\n 1 a\n 2 a\n" >expected
atf_check_uniq -D -c
atf_check_uniq -Dnone -c
atf_check_uniq -cD
atf_check_uniq -cDnone
atf_check_uniq -c -D
atf_check_uniq -c -Dnone
atf_check_uniq --all-repeated --count
atf_check_uniq --all-repeated=none --count
atf_check_uniq --count --all-repeated
atf_check_uniq --count --all-repeated=none
}
atf_test_case skip_fields
@ -183,6 +215,7 @@ atf_init_test_cases()
atf_add_test_case repeated
atf_add_test_case count_repeated
atf_add_test_case all_repeated
atf_add_test_case count_all_repeated
atf_add_test_case skip_fields
atf_add_test_case skip_fields_tab
atf_add_test_case ignore_case

View file

@ -30,7 +30,7 @@
.\"
.\" From: @(#)uniq.1 8.1 (Berkeley) 6/6/93
.\"
.Dd January 12, 2024
.Dd December 9, 2024
.Dt UNIQ 1
.Os
.Sh NAME
@ -38,8 +38,8 @@
.Nd report or filter out repeated lines in a file
.Sh SYNOPSIS
.Nm
.Op Fl c | Fl d | Fl D | Fl u
.Op Fl i
.Op Fl cdiu
.Op Fl D Ns Op Ar septype
.Op Fl f Ar num
.Op Fl s Ar chars
.Oo

View file

@ -237,12 +237,13 @@ main (int argc, char *argv[])
fputc('\n', ofp);
show(ofp, prevline);
}
show(ofp, thisline);
} else if (dflag && !cflag) {
if (repeats == 0)
show(ofp, prevline);
}
++repeats;
if (Dflag)
show(ofp, thisline);
}
}
if (ferror(ifp))
@ -378,7 +379,7 @@ obsolete(char *argv[])
static void
usage(void)
{
(void)fprintf(stderr,
"usage: uniq [-c | -d | -D | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
(void)fprintf(stderr, "usage: uniq [-cdiu] [-D[septype]] "
"[-f fields] [-s chars] [input [output]]\n");
exit(1);
}