awk to re-summarize

Source: Internet
Author: User

awk to re-summarize:

Read the http://blog.chinaunix.net/uid-2598756-id-293231.html article and conclude that awk is going to weigh

File1

AAA 111

BBB 222

CCC 333

DDD 444

AAA 111

BBB 222

Method One:

awk ' ++a[$0]==1{print $} ' file1 or awk ' {if (++a[$0]==1) print $} ' file1

Explain the above method:

If Awk is involved in judgment, you can write the judgment outside of the action, write it out without if, write the conditions to judge directly, then the array interpretation, we all know if a text string is the first occurrence, then his value is 0, so AAA 111 This string at the first occurrence of the a[$0] result is 0, and then the result + + operation, the result becomes 1, thus output. If the second occurrence, the result of a[$0] is 1, after the + + operation, the result is not 1, so do not output. The duplicate items are removed.

Method Two:

awk '!a[$0]++{print $} ' file1

Explanatory notes:

This is also the classic way to go heavy, if the first time, a[$0] results for the result of 0,! is 1, in the operation + + operations A[$0] The result is changed to 1, thus the output. But if not for the first time, the result of a[$0] is not 1, the reverse is the result of 0,0++ or 0, so it will not be output.

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.