awk + how to count words in the first field

yael

I have file as the following file on rhel 7.2 machine

more file.txt


car12
car55
car87
car12
car98
car55
car12
car12
car55
car65
car12
car65

we want to sum the words in the file with the quantity as the follwing expected results

car12 - 5
car55 - 3
car87 - 1
car98 - 1
car65 - 2

how to sum the above words with their quantity with awk ?

terdon

You don't really need awk for this, you can simply do:

$ sort file | uniq -c
      5 car12
      3 car55
      2 car65
      1 car87
      1 car98

But yes, it is possible in awk also:

$ awk '{a[$1]++}END{for(word in a){print word" - "a[word]}}' file 
car55 - 3
car65 - 2
car87 - 1
car12 - 5
car98 - 1

With GNU awk (gawk), one may even use a few predefined sortings regarding how the array is traversed, and by extension how it is printed. If you use PROCINFO["sorted_in"]="@ind_str_asc", that will cause the results to be printed in the order from most seen to least:

$ awk '{a[$1]++} 
       END { 
        PROCINFO["sorted_in"]="@ind_str_asc";
        for(word in a){print word" - "a[word]}
       }' file
car12 - 5
car55 - 3
car65 - 2
car87 - 1
car98 - 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count words input field

From Dev

Count words input field

From Dev

awk field count arithmetic

From Dev

AWK: how to select lines by the number of words in one field?

From Dev

How to count same words in a string and get the index of the first word that is equal?

From Dev

How to group by first 2 words in a pandas dataframe column and count?

From Dev

Select distinct first words of a field

From Dev

How can I add a string to the first field of the output with awk?

From Dev

how can I substite a , in the first field with awk when the fieldseparator is also a ,

From Dev

awk to count and rename symbol in field

From Dev

How to get count from first table field when joining

From Dev

How to count words in a line

From Dev

count by field in awk before pipe symbol

From Dev

How to replace words after first two words

From Dev

awk field separator not working for first line

From Dev

Using the first field in AWK as file name

From Dev

Using the first field in AWK as file name

From Dev

awk replace part of string in first field

From Dev

Print the first and last match of a field with awk

From Dev

awk: transpose column header to first field of row

From Dev

How to validate words count in laravel

From Dev

How to count the number of words in a variable?

From Dev

How to count words and group by hour?

From Dev

How to count words in a list of lists?

From Dev

How to Count Number of in in a field

From Dev

How to get first 2 words?

From Dev

awk if first field occurs multiple times comma separate second field

From Dev

Find difference in second field, report using first field (awk)

From Java

cut or awk command to print first field of first row

Related Related

HotTag

Archive