pattern check using awk

Viewed 43

Is there a way to use awk or python to check patterns, in our group we have 120,000 lines messages but what to know who replies after who the most;

Consider the following lines;

  • AA
  • BB
  • AA
  • CC
  • AA
  • BB
  • AA
  • AA
  • AA
  • BB
  • CC
  • AA

I would want the result to show

  • AA->BB = 3
  • BB->AA = 2
  • AA->CC = 1
  • CC->AA = 2
  • AA->CC = 1
  • BB->CC = 1
  • CC->BB = 0
2 Answers

Something like this might be sufficient for you:

$ awk 'NR > 1 && prev != $1 { sum[prev"->"$1]++ }
       { prev = $1 }
       END { for (a in sum) { print a" = "sum[a] } }
  ' input.txt
AA->CC = 1
BB->AA = 2
AA->BB = 3
BB->CC = 1
CC->AA = 2
  • Sorting is random,
  • it doesn't print AA->CC = 1 twice ;)
  • and it doesn't print empty relations CC->BB = 0
$ awk -v OFS="=" '                           # set for one single use
{
    if(FNR>1 && p!=$0)                       # exclude unwanted
        a[p "->" $0]++                       # count
    p=$0                                     # save $0 for next round
}
END {                                        # in the end
    PROCINFO["sorted_in"]="@val_num_desc"    # ordering in GNU awk
    for(i in a)                              # We loop
        print i,a[i]                         # and count
}' file

Output:

AA->BB=3
BB->AA=2
CC->AA=2
AA->CC=1
BB->CC=1

If you want the output sorted and not the implementation related awk default and you are using GNU awk, use PROCINFO["sorted_in"]="@val_num_desc". If not, remove the line and pipe the output to sort -t= -k2n

Related