Code for giving the frequency of numbers appearing in rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Code for giving the frequency of numbers appearing in rows
# 1  
Old 03-25-2010
Question Code for giving the frequency of numbers appearing in rows

I have a data file that has 6 columns and I have sorted the 3rd column so that all rows are sorted according to the 3rd column (from lowest to highest number). An example is shown below:

7563 273 1 -15.81100000 25.37250000 -19.27320000
8149 294 1 -17.90540000 -6.59144000 -26.03650000
8242 297 2 -13.49870000 19.54790000 -13.56060000
10003 362 3 -13.49950000 23.41270000 -21.26740000
10049 363 4 -12.82110000 14.94900000 -20.00750000
10080 364 5 -12.91600000 2.18556000 -26.58310000
10096 365 5 -15.61200000 4.07117000 -22.90540000
10142 366 6 -13.30700000 10.08880000 -23.34130000
10173 367 6 -18.49690000 13.56170000 31.67000000

I am trying to write a code in awk that will scan through the rows and give me output like how many times each number is appearing in the 3rd column i.e. the frequency with which numbers are appearing. The first column can be the number and the 2nd column is the number of times its appearing. I want the output like this (in reference to data above). I am new to awk so I need some guidance.

1 2
2 1
3 1
4 1
5 2
6 2
# 2  
Old 03-25-2010
Code:
awk '{a[$3]++} END {for (i in a) print i, a[i]|"sort -n"}' urfile

# 3  
Old 03-25-2010
Bug

thanks rdcwayx,
the code worked perfectly...........thanks a lot.
# 4  
Old 03-25-2010
Since the file is sorted you can also do something like:
Code:
awk 'c && s!=$3{print s,c;c=0} {s=$3;c++} END {print s,c}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Summing up values of rows of numbers

data file contains failed=24 error=23 error=163 failed=36 error=903 i need to get a total count of each value above. i'm looking for the most efficient method to do this as the datafile i provided is just a sample. the actual data can be several hundred thousands of lines. so from... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Code for count the frequency of interacting pairs

Hi all, I am trying to analyze my data, and I will need your experience. I have some files with the below format: res1 = TYR res2 = ASN res1 = ASP res2 = SER res1 = TYR res2 = ASN res1 = THR res2 = LYS res1 = THR res2 = TYR etc (many lines) I am... (3 Replies)
Discussion started by: Tzole
3 Replies

3. UNIX for Dummies Questions & Answers

Assigning rank to rows of numbers with directionality

I have a tab-delimited text file where the second column contains in increasing order and includes negative and positive numbers. I want to assign ranks to the rows based on the value of the second column. The tricky part is that the row where the second column is "0", will be assigned "0", the... (3 Replies)
Discussion started by: evelibertine
3 Replies

4. UNIX for Dummies Questions & Answers

Assigning rank to rows of numbers based on the last column

I have a tab delimited text file that looks like the following: ERBB3 0.00097 IL31RA 0.000972 SETD5 0.000972 MCART1 0.000973 CENPJ 0.000973 FNDC6 0.000974 I want to assign a number to each row based on the value in the last column (in the order of increasing value so that the first row... (3 Replies)
Discussion started by: evelibertine
3 Replies

5. UNIX for Dummies Questions & Answers

Frequency of a range of numbers

Hello, I have a column where there are values from 1 to 150. I want to get the frequency of values in the following ranges: 1-5 6-10 11-15 .... .... .... 146-150 How can I do this in a for loop? Thanks, Guss (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

6. Shell Programming and Scripting

Insert rows based on line numbers

Can I insert rows based on line numbers. Say If I need to insert 1 or more rows in a file from line number 10. Can I do that in UNIX I have a file something like A B C D E F After row C, I wanted to add 2 records as X and Y. I have the line number after C as my reference. Can I... (2 Replies)
Discussion started by: Muthuraj K
2 Replies

7. Shell Programming and Scripting

Delete rows based on line numbers in a file

I have to find the number of rows in a file and delete those many rows in another file. For example, if I have 3 rows in a file A, i have to delete first 3 rows in anothe file B, I have the code, it works as standalone, when I merge this with m application (c with unix), it doesnt work. ... (2 Replies)
Discussion started by: Muthuraj K
2 Replies

8. Shell Programming and Scripting

Reversing numbers in a datafile of rows and columns

Hello, I've tried searching the forum for an answer to my question, but without any luck... I have a datafile looking simplified as follows: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 I want to reverse it by rearranging all the numbers from last to... (16 Replies)
Discussion started by: mattings
16 Replies

9. UNIX for Dummies Questions & Answers

Find different column numbers among rows in data

I want to find the different column numbers among rows in a file. For example: A001 a b c d e ... N A002 a b c d e ... N A003 a b c d e ... N+1 A004 a b c d e ... N A005 a b c d e ... N+2 : : For most of the lines I will have N columns (say 1000 rows) in each line except the line 3... (5 Replies)
Discussion started by: AMBER
5 Replies
Login or Register to Ask a Question