Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding gaps in sequential numbers

Tags:

bash

awk

I don’t do this stuff for a living so forgive me if it’s a simple question (or more complicated than I think). I‘ve been digging through the archives and found a lot of tips that are close but being a novice I’m not sure how to tweak for my needs or they are way beyond my understanding.

I have some large data files that I can parse out to generate a list of coordinate that are mostly sequential

5 6 7 8 15 16 17 25 26 27 

What I want is a list of the gaps

1-4 9-14 18-24 

I don’t know perl, SQL or anything fancy but thought I might be able to do something that would subtract one number from the next. I could then at least grep the output where the difference was not 1 or -1 and work with that to get the gaps.

like image 945
Shaun Avatar asked Apr 07 '13 20:04

Shaun


People also ask

How do I find the gaps in a sequence in Excel?

Click the first cell in an empty column, hold the "Shift" key and click the last data cell in that column. This selects all cells between those two points. You need at least as many cells selected as the anticipated number of missing values.


2 Answers

With awk :

awk '$1!=p+1{print p+1"-"$1-1}{p=$1}' file.txt 

explanations

  • $1 is the first column from current input line
  • p is the previous value of the last line
  • so ($1!=p+1) is a condition : if $1 is different than previous value +1, then :
  • this part is executed : {print p+1 "-" $1-1} : print previous value +1, the - character and fist columns + 1
  • {p=$1} is executed for each lines : p is assigned to the current 1st column
like image 194
Gilles Quenot Avatar answered Sep 28 '22 18:09

Gilles Quenot


interesting question.

sputnick's awk one-liner is nice. I cannot write a simpler one than his. I just add another way using diff:

 seq $(tail -1 file)|diff - file|grep -Po '.*(?=d)' 

the output with your example would be:

1,4 9,14 18,24 

I knew that there is comma in it, instead of -. you could replace the grep with sed to get -, grep cannot change the input text... but the idea is same.

hope it helps.

like image 38
Kent Avatar answered Sep 28 '22 18:09

Kent