4

I have a file that needs organized a number of repeats, my file has 6 rows and 3120 lines each member has 1 or 2 digits, total 18720 numbers with one or 2 digits.

I would like to count how many 1, how many 2, how many 3 so on to how many 45 I have in my file and output organanize most reported number to less repeated. Example:

5   7   13  25  26  44
12  21  26  28  32  44
10  20  22  26  40  44
13  18  19  20  22  23
9   15  17  19  22  43
10  19  28  29  37  45
2   8   12  13  22  35
3   23  24  26  38  39
3   14  19  20  33  43
2   3   8   35  36  43
2   8   25  29  37  43
3   24  27  29  38  44
5   20  23  32  33  40
11  17  22  26  33  36
1   6   31  32  39  44
4   22  27  31  36  43
3   6   18  22  35  44
11  13  24  28  32  33
17  22  27  29  42  44
8   9   16  23  29  44
13  19  20  33  37  40
18  21  29  31  34  44
14  15  20  31  32  41
6   32  33  40  41  43
11  17  18  31  43  44
1   9   10  22  24  34
6   8   9   35  37  38
14  18  21  36  40  43
11  16  17  32  34  38
1   5   14  22  23  24
5   19  21  22  32  45
12  17  20  22  32  44
9   27  32  38  42  44
4   12  16  26  42  45
6   7   8   16  39  42
5   6   13  18  27  28

Output:

most repeted first with number of repetion

5=30 Five reapeted 30 times

4=28 Four reapeted 28 times

.........

.........

.........

35=0 Thirty five repeated 0 times

I try this but doesn't work and I'm not expert to change code

$ tr -s " " "\n" | sort | uniq -c | sort -n r | awk '{print $2 " = " $1}'
$ awk -v RS='\\s+' '{cnt[$0]++} END{PROCINFO["sorted_in"]="@val_num_desc"; for (i in cnt) print i" = "cnt[i]}' file
$ tr -s ' ' '\n' <file | sort | uniq -c | sort -nr | awk '{print $2 " = " $1}'
$ tr -s ' ' '\n' <nums | sort | uniq -c | sort -k1,1nr -k2n | awk '{print $2 " = " $1}'
$ awk -v RS=" +|\n" '{a[$1]++}END{for(x in a)printf "%d = %d\n",x,a[x]}' file
$ awk -v RS=" +|\n" '{a[$1]++}END{for(x in a)printf "%d = %d\n",x,a[x]}' f|sort -nr -k3
John
  • 171

3 Answers3

6

You can do:

tr -s ' ' <file.txt | tr ' ' '\n' | sort -n | uniq -c | sort -k1,1rn -k2,2rn | sed 's/^ \+//; s/ /=/'
  • tr -s ' ' will make successive spaces into one

  • tr ' ' '\n' will convert spaces into newlines

  • sort -n will do numeric sort-ing

  • uniq -c will do the count

  • sort -k1,1rn -k2,2rn | sed 's/^ \+//; s/ /=/' will format the output the way you want.

Example:

$ tr -s ' ' <file.txt | tr ' ' '\n' | sort -n | uniq -c | sort -k1,1rn -k2,2rn | sed 's/^ \+//; s/ /=/'
12=44
12=22
10=32
8=43
7=20
6=33
6=29
6=26
6=19
6=18
6=17
6=13
6=8
6=6
5=40
5=38
5=31
5=27
5=24
5=23
5=9
5=5
5=3
4=42
4=37
4=36
4=35
4=28
4=21
4=16
4=14
4=12
4=11
3=45
3=39
3=34
3=10
3=2
3=1
2=41
2=25
2=15
2=7
2=4
heemayl
  • 93,925
6

This produces the a count of the number of occurrences of a number, sorted in descending order by number of occurrences:

$ awk '{for (i=1;i<=NF;i++) a[$i]++;} END{for (i in a)print i"="a[i]}' file | sort -rnt= -k2,2
44=12
22=12
32=10
43=8
20=7
8=6
6=6
33=6
29=6
26=6
19=6
18=6
17=6
13=6
[...snip...]

In the above, 44=12 means that 44 was repeated 12 times.

GNU awk

If you have GNU awk (gawk) installed, you can eliminate the need for a pipeline and do it all in one awk statement:

awk 'BEGIN{PROCINFO["sorted_in"]="@val_num_desc"} {for (i=1;i<=NF;i++) a[$i]++;} END{for (i in a)print i"="a[i]}' file
John1024
  • 13,947
0

Just an AWK variation (I haven't AWK for a long time...)

awk -v RS="[ \n]+" '{a[$1]++} END{ for(b in a){print b,a[b] | "sort -rnk2,2"}}'