Counting a consecutive number in column 2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting a consecutive number in column 2
# 1  
Old 02-06-2014
Counting a consecutive number in column 2

Hi,
I have a input file which contains following data

Code:
0 1
0 2
0 3
0 4
0 8 
0 9
0 11
1 1
1 2
1 6
1 7
1 8
1 9
2 1
2 11
2 12 
2 13

I would like to count a consecutive number in column 2 which are grouped by column 1 as well as obtain the maximum number of consecutive no. in each group.

So, results I want to get would be

Code:
0 4
0 2
0 1
1 2
1 4
2 1
2 3

As for maximum thing,

Code:
0 4
1 4
2 3

Thanks in advance
# 2  
Old 02-06-2014
are you sure this isn't a homework?
# 3  
Old 02-06-2014
No. It is the result from my research

Quote:
Originally Posted by th0gz19
are you sure this isn't a homework?
That is a part of result which represents the stacking interaction in DNA. Column 1 represents time, Column 2 is residue in DNA participating stacking interaction. I just want to know the how many stacks are formed in a consecutive manner.
Since raw data is too complex to post here, I just omitted raw data and gave the example in order to explain more efficiently.
# 4  
Old 02-06-2014
Try something like:
Code:
awk '$1>p || $2!=q+1{if(NR>1)print p,c; c=0} {p=$1; q=$2; c++} END{print p,c}' file


Last edited by Scrutinizer; 02-06-2014 at 05:41 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-06-2014
Thanks for your help. I have one more question

Quote:
Originally Posted by Scrutinizer
Try something like:
Code:
awk '$1>p || $2!=q+1{print p,c; c=0} {p=$1; q=$2; c++} END{print p,c}' file

Thanks. It really works.

Also, I would appreciate it a lot if you reply me how to obtain maximum value in the group.

The result I would like to get is following

Code:
0 4
1 4
2 3

# 6  
Old 02-06-2014
Try something like:
Code:
awk '$1>p || $2!=q+1{if(c>m)m=c; c=0} $1>p{if(NR>1)print p,m; m=0} {p=$1; q=$2; c++} END{if(c>m)m=c; print p,m}' file


Last edited by Scrutinizer; 02-06-2014 at 05:40 PM.. Reason: Also set max in the END section
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 02-06-2014
Sincerely appreciate it !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check/print missing number in a consecutive range and remove duplicate numbers

Hi, In an ideal scenario, I will have a listing of db transaction log that gets copied to a DR site and if I have them all, they will be numbered consecutively like below. 1_79811_01234567.arc 1_79812_01234567.arc 1_79813_01234567.arc 1_79814_01234567.arc 1_79815_01234567.arc... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Cut counting consecutive delimiters as fields

When cut encounters consecutive delimiters it seems to count each instance as a field, at least with spaces. Is this typical behavior for any delimiter? #:~$ ifconfig eth0 | grep HWaddr eth0 Link encap:Ethernet HWaddr 94:de:80:a7:6d:e1 #:~$ ifconfig eth0 | grep HWaddr | cut -d " " -f... (6 Replies)
Discussion started by: Riker1204
6 Replies

3. Shell Programming and Scripting

Delete a specific line containing non consecutive number?

Dear Specialists, I have following data 1 1 2 2 2 3 3 3 6 4 3 4 5 4 9 6 5 11 7 6 7 and I would like to obtain data like below 1 1 2 2 2 3 4 3 4 7 6 7 (2 Replies)
Discussion started by: Ryan Kim
2 Replies

4. Shell Programming and Scripting

Counting the number of element in each column

Hello, I have a file as follows: ENSGALG00000000189 ENSGALG00000000189 ENSGALG00000000189 ENSGALG00000000215 ENSGALG00000000215 ENSGALG00000000218 ... (5 Replies)
Discussion started by: Homa
5 Replies

5. UNIX for Dummies Questions & Answers

Sum every 3 consecutive numbers in a column

Dear All, I have a file with only one column. And I want to add every 3 consecutive numbers together and print the result. Input File: 21.1 10 10 55 11 99 10 8 4 Expected Output: 41.1 (5 Replies)
Discussion started by: NamS
5 Replies

6. Shell Programming and Scripting

Number of Consecutive Occurences

:b: Hi Folks, I have a requirement to find the number of consecutive occurences based on month. Here is the below sample of my requirement. I have say four columns CARUSERNAME BRAND_CODE MONTH YEAR Below is sample Data John|C|JAN|2013 Tim|C|FEB|2013 Tom|C|Mar|2013 Ron|C|Apr|2013... (4 Replies)
Discussion started by: dinesh1985
4 Replies

7. Shell Programming and Scripting

How to compare the values of a column in awk in a same file and consecutive lines..

I would like to compare the values of 2nd column of consecutive lines of same file in such a way so that if the difference between first value and second value is more than 100 it should print complete line else ignore line. Input File ========== PDB 2500 RTDB 123 RTDB-EAGLE 122 VSCCP 2565... (4 Replies)
Discussion started by: manuswami
4 Replies

8. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

9. Shell Programming and Scripting

Calculate difference between consecutive data points in a column from a file

Hi, I have a file with one column data (sample below) and I am trying to write a shell script to calculate the difference between consecutive data valuse i.e Var = Ni -N(i-1) 0.3141 -3.6595 0.9171 5.2001 3.5331 3.7022 -6.1087 -5.1039 -9.8144 1.6516 -2.725 3.982 7.769 8.88 (5 Replies)
Discussion started by: malandisa
5 Replies

10. Shell Programming and Scripting

counting number of sentence

Hi all I want to count total numbers of sentences separated by fullstop (.) in different files under a directory at one go. Any help is appreciated. (3 Replies)
Discussion started by: my_Perl
3 Replies
Login or Register to Ask a Question