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