Linux command: Uniq

Source: Internet
Author: User

uniq Command Introduction:

This command reads the input file and compares adjacent rows.


1. command format:

Uniq [OPTION] ... [INPUT [OUTPUT]]


2. Command function:

The second and later repeating rows are deleted, and the row comparison is based on the sort sequence of the character set used. The result of the command processing is written to the output file. The input file and output file must be different. If the input file is represented by "-", it is read from the standard input.


3. Command parameters:

–c Displays the output, at the beginning of each line, plus the number of times the bank appears in the file. It can replace the-U and-D options.

–d Displays only duplicate rows.

–u Displays only the rows that are not duplicated in the file.

–n The first n fields are ignored along with the white space in front of each field. A field is a non-whitespace, non-tabbed string,

Each other is separated by tabs and spaces (fields are numbered starting with 0).

+n The first n characters are ignored, the previous characters are skipped (the characters are numbered starting with 0).

–f N is the same as-N, where n is the number of fields.

–s N is the same as +n, where n is the number of characters.


4. Command instance:


Example One:uniq test

[[email protected] ~]# Cat Testboy took bat Homeboy took bat Homegirl took bat Homeboy took bat Homeboy took bat Homedog b Rought hat Homedog brought hat Homedog brought hat home

The Uniq command does not add any parameters, only one row of consecutive duplicates is displayed

[Email protected] ~]# Uniq Testboy took bat Homegirl took bat Homeboy took bat Homedog brought hat home


Example two:uniq-c test

[[email protected] ~]# uniq-c test2 boy took bat home1 girl took bat Home2 boy took bat Home3 dog brought hat home1

The-c parameter displays the number of consecutive occurrences of each row in the file.


example three: sorting and then displaying

[email protected] ~]# Cat Test |sort | Uniq-c14 boy took bat Home3 dog brought hat home1 girl took bat home


example four: displaying consecutive occurrences of a row

[Email protected] ~]# uniq-d Testboy took Bat Homeboy took bat Homedog brought hat home


Example five: show rows that do not appear consecutively

[Email protected] ~]# uniq-u Testgirl took bat home


example Six: ignores the first 2 fields of each row, ignores the second white space character and the first character of the third field, and results at home

[[email protected] ~]# Cat Testboy took bat Homeboy took bat Homegirl took bat Homeboy took bat Homeboy took bat Homedog b Rought hat Homedog brought hat Homedog brought hat Home[[email protected] ~]# uniq-f 2-s 2 Testboy took bat home


Instance seven: Ignores the first field of each row, so that boy, the line beginning with girl, looks like a row that repeats continuously

[Email protected] ~]# uniq-f 1 Testboy took bat Homedog brought hat home


Example Eight:Displays all duplicate rows, each repeating row showing
[[email protected] ~]# uniq-d Testboy took Bat Homeboy took bat Homeboy took bat Homeboy took bat Homedog brought hat hom Edog brought hat Homedog brought hat home

Linux command: Uniq

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.