Given an input file containing one single number per line, how could I get a count of how many times an item occurred in that file?
cat input.txt
1
2
1
3
1
0
desired output (=>[1,3,1,1]):
cat output.txt
0 1
1 3
2 1
3 1
It would be great, if the solution could also be extended for floating numbers.
Another option:
Loop over each line with
-n
Each
$_
number increments hash%h
Once the
END
ofinput.txt
has been reached,sort {$a <=> $b}
the hash numericallyPrint the number
$n
and the frequency$h{$n}
Similar code which works on floating point:
float.txt
output:
Using
maphimbu
from the Debian stda package:Output:
maphimbu
also works with floating point:Output:
In addition to the other answers, you can use awk to make a simple graph. (But, again, it's not a histogram.)
At least some of that can be done with
But the order
number count
is reversed. This will fix that problem.You mean you want a count of how many times an item appears in the input file? First sort it (using
-n
if the input is always numbers as in your example) then count the unique results.