generación de histograma a partir de archivo


Dado un archivo de entrada que contiene un solo número por línea, ¿cómo podría obtener un recuento de cuántas veces se produjo un elemento en ese archivo?

cat input.txt
1
2
1
3
1
0

Salida Deseada (=>[1,3,1,1]):

cat output.txt
0 1
1 3
2 1
3 1

Sería genial, si la solución también se pudiera extender para números flotantes.

Author: Javier, 2011-05-18

6 answers

¿Quiere decir que desea contar cuántas veces aparece un elemento en el archivo de entrada? Primero ordénelo (usando -n si la entrada es siempre números como en su ejemplo) luego cuente los resultados únicos.

sort -n input.txt | uniq -c
 75
Author: Caleb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-18 12:26:09

Otra opción:

awk '{n[$1]++} END {for (i in n) print i,n[i]}' input.txt | sort -n > output.txt
 10
Author: glenn jackman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-18 13:17:29

Al menos algo de eso se puede hacer con

sort output.txt | uniq -c

Pero el orden number count se invierte. Esto solucionará ese problema.

sort test.dat | uniq -c | awk '{print $2, $1}'
 1
Author: pavium,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-20 00:02:14

Además de las otras respuestas, puede usar awk para hacer un gráfico simple. (Pero, de nuevo, no es un histograma.)

 1
Author: Mike Sherrill 'Cat Recall',
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 10:31:35

Usando maphimbu desde Debian stda paquete:

# use 'jot' to generate 100 random numbers between 1 and 5
# and 'maphimbu' to print sorted "histogram":
jot -r 100 1 5 | maphimbu -s 1

Salida:

             1                20
             2                21
             3                20
             4                21
             5                18

maphimbu también funciona con coma flotante:

jot -r 100.0 10 15 | numprocess /%10/ | maphimbu -s 1

Salida:

             1                21
           1.1                17
           1.2                14
           1.3                18
           1.4                11
           1.5                19
 0
Author: agc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-31 12:49:22
perl -lne '$h{$_}++; END{for $n (sort keys %h) {print "$n\t$h{$n}"}}' input.txt

Bucle sobre cada línea con -n
Cada $_ número incrementa hash %h
Una vez que se ha alcanzado el END de input.txt ,
sort {$a <=> $b} el hash numéricamente
Imprime el número $n y la frecuencia $h{$n}

Código similar que funciona en coma flotante:

perl -lne '$h{int($_)}++; END{for $n (sort {$a <=> $b} keys %h) {print "$n\t$h{$n}"}}' float.txt

Flotar.txt

1.732
2.236
1.442
3.162
1.260
0.707

Salida:

0       1
1       3
2       1
3       1
 0
Author: Chris Koknat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-04 18:10:58