frequency count for file column in bash

后端 未结 3 1021
滥情空心
滥情空心 2021-01-25 18:12

I have a file with 8 columns using | as a delimiter and I want to count the occurence frequency of the words in the 8th column. I tried awk like this



        
3条回答
  •  广开言路
    2021-01-25 18:29

    Among other things, you're running uniq on $FILE instead of running awk on $FILE and piping the results to sort then uniq. You meant to write:

    awk -F'|' '{print $8}' "$FILE" | sort | uniq -c
    

    but all you need is one command:

    awk -F'|' '{cnt[$8]++} END{for (key in cnt) print cnt[key], key}' "$FILE"
    

    wrt I can't understand where this "1" come from - you have 1 empty $8 in your input file. Maybe a blank line. You can find it with:

    awk -F'|' '$8~/^[[:space:]]*$/{print NR, "$0=<"$0">, $8=<"$8">"}' "$FILE"
    

提交回复
热议问题