xref: /freebsd/usr.bin/uniq/uniq.1 (revision bdcbfde31e8e9b343f113a1956384bdf30d1ed62)
1.\" Copyright (c) 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the Institute of Electrical and Electronics Engineers, Inc.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.Dd June 7, 2020
32.Dt UNIQ 1
33.Os
34.Sh NAME
35.Nm uniq
36.Nd report or filter out repeated lines in a file
37.Sh SYNOPSIS
38.Nm
39.Op Fl c | Fl d | Fl D | Fl u
40.Op Fl i
41.Op Fl f Ar num
42.Op Fl s Ar chars
43.Oo
44.Ar input_file
45.Op Ar output_file
46.Oc
47.Sh DESCRIPTION
48The
49.Nm
50utility reads the specified
51.Ar input_file
52comparing adjacent lines, and writes a copy of each unique input line to
53the
54.Ar output_file .
55If
56.Ar input_file
57is a single dash
58.Pq Sq Fl
59or absent, the standard input is read.
60If
61.Ar output_file
62is absent, standard output is used for output.
63The second and succeeding copies of identical adjacent input lines are
64not written.
65Repeated lines in the input will not be detected if they are not adjacent,
66so it may be necessary to sort the files first.
67.Pp
68The following options are available:
69.Bl -tag -width Ds
70.It Fl c , Fl -count
71Precede each output line with the count of the number of times the line
72occurred in the input, followed by a single space.
73.It Fl d , Fl -repeated
74Output a single copy of each line that is repeated in the input.
75.It Fl D , Fl -all-repeated Op Ar septype
76Output all lines that are repeated (like
77.Fl d ,
78but each copy of the repeated line is written).
79The optional
80.Ar septype
81argument controls how to separate groups of repeated lines in the output;
82it must be one of the following values:
83.Pp
84.Bl -tag -compact -width separate
85.It none
86Do not separate groups of lines (this is the default).
87.It prepend
88Output an empty line before each group of lines.
89.It separate
90Output an empty line after each group of lines.
91.El
92.It Fl f Ar num , Fl -skip-fields Ar num
93Ignore the first
94.Ar num
95fields in each input line when doing comparisons.
96A field is a string of non-blank characters separated from adjacent fields
97by blanks.
98Field numbers are one based, i.e., the first field is field one.
99.It Fl i , Fl -ignore-case
100Case insensitive comparison of lines.
101.It Fl s Ar chars , Fl -skip-chars Ar chars
102Ignore the first
103.Ar chars
104characters in each input line when doing comparisons.
105If specified in conjunction with the
106.Fl f , Fl -unique
107option, the first
108.Ar chars
109characters after the first
110.Ar num
111fields will be ignored.
112Character numbers are one based, i.e., the first character is character one.
113.It Fl u , Fl -unique
114Only output lines that are not repeated in the input.
115.\".It Fl Ns Ar n
116.\"(Deprecated; replaced by
117.\".Fl f ) .
118.\"Ignore the first n
119.\"fields on each input line when doing comparisons,
120.\"where n is a number.
121.\"A field is a string of non-blank
122.\"characters separated from adjacent fields
123.\"by blanks.
124.\".It Cm \&\(pl Ns Ar n
125.\"(Deprecated; replaced by
126.\".Fl s ) .
127.\"Ignore the first
128.\".Ar m
129.\"characters when doing comparisons, where
130.\".Ar m
131.\"is a
132.\"number.
133.El
134.Sh ENVIRONMENT
135The
136.Ev LANG ,
137.Ev LC_ALL ,
138.Ev LC_COLLATE
139and
140.Ev LC_CTYPE
141environment variables affect the execution of
142.Nm
143as described in
144.Xr environ 7 .
145.Sh EXIT STATUS
146.Ex -std
147.Sh EXAMPLES
148Assuming a file named cities.txt with the following content:
149.Bd -literal -offset indent
150Madrid
151Lisbon
152Madrid
153.Ed
154.Pp
155The following command reports three different lines since identical elements
156are not adjacent:
157.Bd -literal -offset indent
158$ uniq -u cities.txt
159Madrid
160Lisbon
161Madrid
162.Ed
163.Pp
164Sort the file and count the number of identical lines:
165.Bd -literal -offset indent
166$ sort cities.txt | uniq -c
167	1 Lisbon
168	2 Madrid
169.Ed
170.Pp
171Assuming the following content for the file cities.txt:
172.Bd -literal -offset indent
173madrid
174Madrid
175Lisbon
176.Ed
177.Pp
178Show repeated lines ignoring case sensitiveness:
179.Bd -literal -offset indent
180$ uniq -d -i cities.txt
181madrid
182.Ed
183.Pp
184Same as above but showing the whole group of repeated lines:
185.Bd -literal -offset indent
186$ uniq -D -i cities.txt
187madrid
188Madrid
189.Ed
190.Pp
191Report the number of identical lines ignoring the first character of every line:
192.Bd -literal -offset indent
193$ uniq -s 1 -c cities.txt
194	2 madrid
195	1 Lisbon
196.Ed
197.Sh COMPATIBILITY
198The historic
199.Cm \&\(pl Ns Ar number
200and
201.Fl Ns Ar number
202options have been deprecated but are still supported in this implementation.
203.Sh SEE ALSO
204.Xr sort 1
205.Sh STANDARDS
206The
207.Nm
208utility conforms to
209.St -p1003.1-2001
210as amended by Cor.\& 1-2002.
211.Sh HISTORY
212A
213.Nm
214command appeared in
215.At v3 .
216