How can I count most occuring sequence of 3 letters within a word with a bash script

后端 未结 3 1272
挽巷
挽巷 2021-01-14 07:31

I have a sample file like

XYZAcc
ABCAccounting
Accounting firm
Accounting Aco
Accounting Acompany
Acoustical consultant

Here I need to grep

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 08:10

    This might work for you (GNU sed, sort and uniq):

    sed -E 's/.(..)/\L&\n\1/;/^\S{3}/P;D' file |
    sort |
    uniq -c |
    sort -s -k1,1rn |
    sed -En 's/^\s*(\S+)\s*(\S+)/\2 = \1/;H;$!b;x;s/\n/ /g;s/.//p'
    

    Use the first sed invocation to output 3 letter lower case words.

    Sort the words.

    Count the duplicates.

    Sort the counts in reverse numerical order maintaining the alphabetical order.

    Use the second sed invocation to manipulate the results into the desired format.


    If you only want lines with duplicates and in alphabetical order and case wise, use:

    sed -E 's/.(..)/&\n\1/;/^\S{3}/P;D' file |
    sort |
    uniq -cd |
    sed -En 's/^\s*(\S+)\s*(\S+)/\2 = \1/;H;$!b;x;s/\n/ /g;s/.//p
    

提交回复
热议问题