![]() We can skip fields and characters before comparing duplicate lines and also consider characters for filtering lines.Īfter reviewing the multiple uniq command options, I would like to share a small image to keep it with you for reference. Just like duplicate lines, we can filter unique lines (non-duplicate lines) as well and can also ignore case sensitivity. The uniq command can count and print the number of repeated lines. Uniq does not detect repeated lines unless they are adjacent. ![]() There is NO WARRANTY, to the extent permitted by law. This is free software: you are free to change and redistribute it. License GPLv3+: GNU GPL version 3 or later. $ uniq -versionĬopyright (C) 2010 Free Software Foundation, Inc. Use the -version option to check the version of the uniq command. Just like skipping characters, we can consider characters as well using the -w option, such as in the example: $ cat file6 Therefore 33aa and 55bb have been discarded. Please keep in mind that the uniq command prints only the first duplicate line and discards other duplicate lines. Just like the field, we can skip characters as well by using the -s option. I’ve given both examples, with and without the -f option, for a better understanding of the option’s behavior: $ cat file5 In the following example, we’re skipping the first field (first column) to compare the duplicate lines from the second field. Sometimes we need to skip some fields to filter duplicate lines. Below I’ve given an output of the uniq command with and without the -i option to compare: $ cat file3 Using the -i option, we can ignore the case sensitivity of characters. Therefore, in the below example, it prints ChhatrapatiShahuMaharaj as an output: $ sort file2 | uniq -uĬhhatrapatiShahuMaharaj With -i, -ignore-case option Opposite of the above option, the -u option prints unique lines i.e., non-duplicate lines. In the below example, the uniq command prints all duplicate lines only and discards non-duplicate lines: $ sort file2 | uniq -D The -D option prints repeated lines and discards the non-duplicate lines. In the below example, I’ve used the -c option to cross-check whether the -d option is only printing the repeated lines or not: $ sort file2 | uniq -cdģ Dr.B.R.Ambedkar With -D, -all-repeated option Therefore, line ChhatrapatiShahuMaharaj has been discarded in the below example: $ sort file2 | uniq -d The -d option prints only lines that are repeated. The below example tells us that the first line is repeated three times, the second line one time, and the third line three times: $ sort file2 | uniq -cģ Dr.B.R.Ambedkar With -d, -repeated option The uniq command prints that count as a prefix with the line. This helps us understand the behavior of the uniq command: $ cat file2īelow, in the next example, we’re using the -c option to count the repeated lines. ![]() In the next example, we take output from a sort command and pipe it with uniq command. In the example, I have tried the uniq command with the original file, but it only prints the output as it is, much like a cat output. Before using the uniq command with this file, we should sort it. Note that this file is not sorted, and the duplicate lines are not adjacent to each other. Without any optionīelow is a file named file2, which contains some data. Some involve only uniq, and others rely on additional commands. ] Examplesīelow are a series of examples, beginning with no options. The lines used in the input file for the uniq command can neither exceed 2048 bytes in length (including any newline characters) nor contain null characters. It actually discards the lines which are repeated and prints the first adjacent repeated line, which enables us to view the output properly. In this scenario, the uniq command helps you to print duplicate lines once in the output.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |