The specified field value with the most occurrences of statistics under Linux

Source: Internet
Author: User

Suppose there is a text called "Data.txt" on the desktop, the following:

{id= ' xxx ' info= ' xxx ' kk= ' xxx ' target= ' 111111 ' dd= ' xxx '}
{id= ' xxx ' info= ' xxx ' kk= ' xxx ' target= ' 777 ' dd= ' xxx '}
{id= ' xxx ' info= ' xxx ' kk= ' xxx ' target= ' yyyy ' dd= ' xxx '}
{id= ' xxx ' info= ' xxx ' kk= ' xxx ' target= ' 5555 ' dd= ' xxx '}
{id= ' xxx ' info= ' xxx ' kk= ' xxx ' target= ' 777 ' dd= ' xxx '}
{id= ' xxx ' info= ' xxx ' kk= ' xxx ' target= ' 555566 ' dd= ' xxx '}

Requirement: Find out the most frequently occurring value of the target field in the "Data.txt" file.

Statistics string under Linux

The Linux terminal is resolved through common tools, as follows:

There are a combination of command lines and tools, including cat, awk, cut, sed, sort, uniq, tail--

Cat

Cat Data.txt, gets and displays the contents of each line of the "Data.txt" file:

Awk

Cat Data.txt | awk ' {print $4} ', showing the fourth field in each row passed in:

Cut

Cat Data.txt | awk ' {print $4} ' | Cut-c 8-, starting from the eighth bit of the passed string, intercepts the string to the last:

Sed

Cat Data.txt | awk ' {print $4} ' | Cut-c 8-| Sed "s/'//g", removing the ' characters contained in the incoming string:

Sort

Cat Data.txt | awk ' {print $4} ' | Cut-c 8-| Sed "s/'//g" | Sort, sorting the results:

Uniq

Cat Data.txt | awk ' {print $4} ' | Cut-c 8-| Sed "s/'//g" | Sort | uniq-c | Sort, go to weight and count the occurrences of each type of string:

Tail

Cat Data.txt | awk ' {print $4} ' | Cut-c 8-| Sed "s/'//g" | Sort | uniq-c | Sort | Tail-n 1, showing the last line in the result:

We are now known to have the most occurrences of the target field value of 777, and the number of occurrences is 2. If there are multiple fields with the same number of occurrences, you can change the value of the tail-n followed by the number to view, and then 10 to display the end of 10 lines.

The specified field value with the most occurrences of statistics under Linux

Related Article

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.