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
# 8  
Old 02-06-2014
You're welcome. I added a modification in the END section. This all only works if column 1 is grouped..
# 9  
Old 02-06-2014
One more question

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

Sorry for bothering you. I would like to know how to get average of the number grouped by column 1.

If I have following data,

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

The result I would like obtain would be

Code:
0 3
1 2

Thanks in advance
# 10  
Old 02-06-2014
Try:
Code:
awk '$1>p {if(NR>1)print p, t/n; t=n=0} {p=$1; n++; t+=$2} END{print p,t/n}'  file

--edit--
I added if(NR>1) in all suggestions

Last edited by Scrutinizer; 02-06-2014 at 05:42 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 11  
Old 04-08-2014
One more question, take two

Hi,
I am working with very similar data and Scrutinizer's answers have been very helpful. However, I was wondering how one would alter the output a bit. For example, if I had Ryan Kim's data in his post 'One more question':
Code:
0 1 
0 2 
0 3 
0 4 
0 5 
1 1 
1 2 
1 3

What if I wanted to print out all of the actual lines that correspond to a series of lines with at least n consecutive values in column 2? For example, if I had n=4 (or 5) and the above data, I would want to extract and print the following lines of data:
Code:
0 1 
0 2 
0 3 
0 4 
0 5

If I had n=3, I would extract all of the lines from the original dataset.

I altered Scrutinizer's awk solution slightly to allow filtering the series based on the number of lines with consecutive values in column two:
Code:
awk '$1>p || $2!=q+1{if(NR>1)print p,c; c=0} {p=$1; q=$2; c++} END{if (c>4) {print p,c}} file

But, I can't figure out how to print the actual series of lines with the consecutive values in them. Any possible advice/explanations would be greatly appreciated!

Thank you very much in advance for any help.

Last edited by mbp; 04-08-2014 at 02:01 AM.. Reason: typo
# 12  
Old 04-08-2014
Something like this?
Code:
awk '
  $1>p || $2!=q+1 {
    if(NR>1 && c>=n) print s
    c=0
    s=x
    p=$1
  }
  {
    s=s ORS $0
    q=$2
    c++
  }
  END{
    if(c>=n)print s
  }
' n=3 file

# 13  
Old 04-09-2014
Great!

Hi Scrutinizer,
thanks very much, that works perfectly!
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