Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating frequency table from file

Tags:

file

bash

shell

awk

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.

like image 873
Javier Avatar asked May 18 '11 12:05

Javier


People also ask

How do you convert data into a frequency table?

Converting from raw data with possible repetitions to a frequency table is relatively simple. First copy the data scores (using Paste Values if necessary) to a new place in the worksheet. Then select this range of cells and click on Data > Data Tools|Remove Duplicates.


1 Answers

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.

sort -n input.txt | uniq -c 
like image 175
Caleb Avatar answered Nov 04 '22 07:11

Caleb