Search code examples
bashshellrangeoverlap

Check for and combine overlapping ranges


How can I check if ranges overlap other ranges and combine the ones that do?

Example:

10-1000,15-350,50-1500,2100,1700-1800,45,40,145,2-1300

The result I want is:

2-1500,1700-1800,2100

I tried to make a plan on how to code it but it's getting me nowhere. Is there a useful package that I can use? Or if not what should my approach be?


Solution

  • Using sort and awk:

    tr , '\n' | sort -n | awk '
    BEGIN {
      FS = OFS = "-"
    }
    NF == 1 {
      $2 = $1
    }
    $2 <= end {
      next
    }
    $1 <= end {
      end = $2
      next
    }
    {
      emit()
      start = $1
      end = $2
    }
    END {
      emit()
    }
    function emit() {
      if (NR != 1) {
        if (start == end)
          print start
        else
          print start, end
      }
    }' | paste -sd,
    
    $ sh ./merge.sh <<<10-1000,15-350,50-1500,2100,1700-1800,45,40,145,2-1300
    2-1500,1700-1800,2100