UNIQ(1P)
๐ฆ PROLOG ๐ฆ
ok so bestie, before we even get into it โ this page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface might not be implemented on Linux at all. ๐ Just so you know what you're getting into.
โจ NAME โจ
uniq โ report or filter out repeated lines in a file ๐
๐ธ SYNOPSIS ๐ธ
uniq [-c|-d|-u] [-f fields] [-s char] [input_file [output_file]]
๐ DESCRIPTION ๐
Okay so here's the vibe: uniq reads an input file, compares adjacent lines, and writes one copy of each input line to the output. The second and succeeding copies of repeated adjacent input lines? Not written. Nope. Gone. ๐ซ
The trailing <newline> of each line in the input gets ignored when it's doing comparisons. Lowkey important.
๐จ MAJOR KEY ALERT: Repeated lines in the input shall not be detected if they are not adjacent. Like, if they're not right next to each other, uniq literally cannot see them. That's just the rules, periodt.
๐ OPTIONS ๐
The uniq utility shall conform to the Base Definitions volume of POSIX.1โ2017, Section 12.2, Utility Syntax Guidelines โ except that '+' may be recognized as an option delimiter as well as '-'. (yes really, both, iconic behavior fr)
The following options shall be supported:
-cโ Precede each output line with a count of the number of times the line occurred in the input. ๐งฎ-dโ Suppress the writing of lines that are not repeated in the input. (only the duplicates survive, main character energy)-ffields โ Ignore the first fields fields on each input line when doing comparisons, where fields is a positive decimal integer. A field is the maximal string matched by the basic regular expression:
[[:blank:]]*[^[:blank:]]*
If the fields option-argument specifies more fields than appear on an input line, a null string shall be used for comparison.
-schars โ Ignore the first chars characters when doing comparisons, where chars shall be a positive decimal integer. If specified in conjunction with the-foption, the first chars characters after the first fields fields shall be ignored. If the chars option-argument specifies more characters than remain on an input line, a null string shall be used for comparison.-uโ Suppress the writing of lines that are repeated in the input. (only the one-of-a-kinds, the uniques, the solo acts โจ)
๐ OPERANDS ๐
The following operands shall be supported:
input_fileโ A pathname of the input file. If the input_file operand is not specified, or if the input_file is'-', the standard input shall be used.output_fileโ A pathname of the output file. If the output_file operand is not specified, the standard output shall be used. The results are unspecified if the file named by output_file is the file named by input_file. ๐ (don't do that, bestie, that's chaos)
๐ STDIN ๐
The standard input shall be used only if no input_file operand is specified or if input_file is '-'. See the INPUT FILES section. ๐
๐ INPUT FILES ๐
The input file shall be a text file. That's it, that's the whole section.
๐ง ENVIRONMENT VARIABLES ๐ง
The following environment variables shall affect the execution of uniq:
LANGโ Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1โ2017, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)LC_ALLโ If set to a non-empty string value, override the values of all the other internationalization variables. (she's the boss ๐)LC_CTYPEโ Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments and input files) and which characters constitute a<blank>in the current locale.LC_MESSAGESโ Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.NLSPATHโ Determine the location of message catalogs for the processing ofLC_MESSAGES.
โญ ASYNCHRONOUS EVENTS โญ
Default. (nothing to see here, moving on ๐จ)
๐ธ STDOUT ๐ธ
The standard output shall be used if no output_file operand is specified, and shall be used if the output_file operand is '-' and the implementation treats the '-' as meaning standard output. Otherwise, the standard output shall not be used. See the OUTPUT FILES section.
๐ STDERR ๐
The standard error shall be used only for diagnostic messages. Like, it's not here to chat, it's here to warn you.
๐ฆ OUTPUT FILES ๐ฆ
If the -c option is specified, the output file shall be empty or each line shall be of the form:
"%d %s", <number of duplicates>, <line>
otherwise, the output file shall be empty or each line shall be of the form:
"%s", <line>
โจ EXTENDED DESCRIPTION โจ
None. ๐
๐ฅ EXIT STATUS ๐ฅ
The following exit values shall be returned:
0โ The utility executed successfully. ๐>0โ An error occurred. ๐ญ
๐ CONSEQUENCES OF ERRORS ๐
Default.
The following sections are informative.
๐ซถ APPLICATION USAGE ๐ซถ
okay so, real talk. If the collating sequence of the current locale has a total ordering of all characters, the sort utility can be used to cause repeated lines to be adjacent in the input file. If the collating sequence does not have a total ordering of all characters, the sort utility should still do this but it might not. ๐
To ensure that all duplicate lines are eliminated, and have the output sorted according the collating sequence of the current locale, applications should use:
LC_ALL=C sort -u | sort
instead of:
sort | uniq
To remove duplicate lines based on whether they collate equally instead of whether they are identical, applications should use:
sort -u
instead of:
sort | uniq
When using uniq to process pathnames, it is recommended that LC_ALL, or at least LC_CTYPE and LC_COLLATE, are set to POSIX or C in the environment, since pathnames can contain byte sequences that do not form valid characters in some locales, in which case the utility's behavior would be undefined. In the POSIX locale each byte is a valid single-byte character, and therefore this problem is avoided. ๐ง
๐ EXAMPLES ๐
The following input file data (but flushed left) was used for a test series on uniq:
#01 foo0 bar0 foo1 bar1
#02 bar0 foo1 bar1 foo1
#03 foo0 bar0 foo1 bar1
#04
#05 foo0 bar0 foo1 bar1
#06 foo0 bar0 foo1 bar1
#07 bar0 foo1 bar1 foo0
What follows is a series of test invocations of the uniq utility that use a mixture of uniq options against the input file data. These tests verify the meaning of adjacent. The uniq utility views the input data as a sequence of strings delimited by '\n'. Accordingly, for the fieldsth member of the sequence, uniq interprets unique or repeated adjacent lines strictly relative to the fields+1th member.
- This first example tests the line counting option, comparing each line of the input file data starting from the second field:
uniq -c -f 1 uniq_0I.t
1 #01 foo0 bar0 foo1 bar1
1 #02 bar0 foo1 bar1 foo1
1 #03 foo0 bar0 foo1 bar1
1 #04
2 #05 foo0 bar0 foo1 bar1
1 #07 bar0 foo1 bar1 foo0
The number '2', prefixing the fifth line of output, signifies that the uniq utility detected a pair of repeated lines. Given the input data, this can only be true when uniq is run using the -f 1 option (which shall cause uniq to ignore the first field on each input line). ๐ฅ
- The second example tests the option to suppress unique lines, comparing each line of the input file data starting from the second field:
uniq -d -f 1 uniq_0I.t
#05 foo0 bar0 foo1 bar1
- This test suppresses repeated lines, comparing each line of the input file data starting from the second field:
uniq -u -f 1 uniq_0I.t
#01 foo0 bar0 foo1 bar1
#02 bar0 foo1 bar1 foo1
#03 foo0 bar0 foo1 bar1
#04
#07 bar0 foo1 bar1 foo0
- This suppresses unique lines, comparing each line of the input file data starting from the third character:
uniq -d -s 2 uniq_0I.t
In the last example, the uniq utility found no input matching the above criteria. (empty output, she said nothing, iconic silence ๐ญ)
๐ง RATIONALE ๐ง
Some historical implementations have limited lines to be 1080 bytes in length, which does not meet the implied {LINE_MAX} limit.
Earlier versions of this standard allowed the -number and +number options. These options are no longer specified by POSIX.1โ2008 but may be present in some implementations. ๐ต (legacy behavior, handle with care)
๐ FUTURE DIRECTIONS ๐
None. (the future is empty, bestie, we live in the now ๐ )
๐ SEE ALSO ๐
comm, sort
The Base Definitions volume of POSIX.1โ2017, Chapter 8, Environment Variables, Section 12.2, Utility Syntax Guidelines
๐ COPYRIGHT ๐
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! this one came to you from IEEE/The Open Group, dated 2017, and it's page UNIQ(1P) in the POSIX Programmer's Manual ๐ซถ go filter some lines, you've got this โจ๐ ๐
๐ฆ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 ยท custom
- alpine 3.23: man-pages-posix 2017a-r0 ยท custom
- alpine 3.24: man-pages-posix 2017a-r0 ยท custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 ยท SUSE-IEEE
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms