Use shell to implement simple word frequency statistics

need:

Count the number of occurrences of the second column of words in the following:

1,huabingood,100
2,haha,200
3,huabingood,300
4,haha,100
5,haha,200

 

Specific code:

cat a.txt | awk -F "," '{print $2}' | sort | uniq -c | sort -nrk 1

Code Explanation:

awk -F "," '{print $2}' # Split the data by commas and take out the contents of the second column
sort # Sort the retrieved content. Because when uniq statistics, if the repeated data is not continuous, it will be considered as two rows with different content
uniq -c # count the number of occurrences of duplicate lines
sort -nrk 1 # Sort in descending numerical order according to the number of repetitions of the first column

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325215633&siteId=291194637