Linux delete duplicate lines of code

Source: Internet
Author: User

Linux Delete duplicate lines of code

In text processing, you often delete duplicate rows, and here are three ways

First, with Sort+uniq, attention, simple uniq is not.

?

1 Sort-n Test.txt | Uniq

Second, with the Sort+awk command, note that simple awk is not the same, for the same reason.

Sort-n $file | awk ' {if ($0!=line) print; line=$0} '

Third, with the sort+sed command, the sort command needs to be sorted first.

Sort-n $file | Sed ' $! N /^.∗n1$/! P D

Shell Script

?

1 2 3 4 5 6 #!/bin/sh file= ' test.txt ' sort-n $file | Uniq Sort-n $file | awk ' {if ($0!=line) print; line=$0} ' Sort-n $file | Sed ' $! N /^ (. *) n1$/! P D

Test files:

yanggang@barry$ Cat Test.txt

Aaa

bbbbb

Ccccc

123

Aaaaa

123

Bbb

Aaa

Execution results:

yanggang@barry$./diffrow.sh

Aaa

Aaaaa

Bbb

bbbbb

Ccccc

123

Server Tutorials

Tablet Computer Tutorials

Video playback Tutorial

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.