how can I detect number series range

Hi
I have one source file and content of source file as follows;

3 00
3 01
3 02
3 07
3 09
3 10
3 15
3 16
3 17
3 18
3 40
3 45
3 500
3 501
3 502
3 70
3 80

How can I detect series of consecutive lines in the source file and get the following output?

3 00 02 (series)
3 07 07
3 09 09
3 10 10
3 15 18 (series)
3 40 40
3 45 45
3 500 502 (series)
3 70 70
3 80 80

Best regards.

Try this...

awk '{if(s==$1&&p+1==$2){v[++j]=p;t=1}else if(t){print s,v[1],p;j=t=0}p=$2;s=$1}' input_file

--ahamed

Hi Ahmed,
your script get only series but uniq lines are need;
output of your script;
3 00 02
3 09 10
3 15 18
3 500 502

I need;
3 00 02
3 07 07 (not found)
3 09 10
3 15 18
3 40 40 (not found)
3 45 45 (not found)
3 500 502
3 70 70 (not found)
3 80 80 (not found)

Best regards

input:

3 00
3 01
3 02
3 07
3 09
3 10
3 15
3 16
3 17
3 18
3 40
3 45
3 500
3 501
3 502
3 70
3 80
#! /usr/bin/perl -w
use strict;
my ($i, @x, $prev);
open I, "< input";
$i=1;
for (<I>) {
    @x = split /\s+/, $_;
    if ($i == 1) {
        $prev = $x[1];
        printf "3 %02d ", $x[1];
        $i++;
        next;
    }
    else {
        if ($x[1] == $prev + 1) {
            $prev = $x[1];
            ($i == $.) && printf "%02d\n", $x[1];
            $i++;
            next;
        }
        else {
            printf "%02d\n", $prev;
            printf "3 %02d ", $x[1];
            ($i == $.) && printf "%02d\n", $x[1];
            $prev = $x[1];
            $i++;
        }
    }
}
close I;

output:

3 00 02
3 07 07
3 09 10
3 15 18
3 40 40
3 45 45
3 500 502
3 70 70
3 80 80

Hi Balajesuri,
Thank you very much
your codes solved my problem. But I do not know Perl, I want to ask two more questions;

1)
You defined as a constant value of the first column value.

your code
printf "3 %02d ", $x[1];

how can I get this value from the source file first column?

2)
I will save your code in a new file and, I want to make an executable script. In this case, I would like to pass the source file name as command line argument, how do I do?

your code
open I, "< input";

Thanks again for your reply.

Try this...

#!/bin/bash
input=$1
awk '{a[++j]=$2;b[j]=$1}
END{
  for(i=1;i<=j;i++){
   if(a[i+1]-a==1 && b==b[i+1]){
     a==0?s="00":0;!s?s=a:0;continue
   } print b,!s?a:s,a;s=0 }
}' $input
root@bt:/tmp# ./run input_file
3 00 02
3 07 07
3 09 10
3 15 18
3 40 40
3 45 45
3 500 502
3 70 70
3 80 80

This also checks for the first number to be same i.e. 3
In case if you don't want that, remove this from the code && b==b[i+1]

--ahamed

Ahamed101 and Balajesuri,
thank you very much. last codes of Ahamed's are solved my problem.
thank you very much again.

  1. Instead of

try this:

(@ARGV != 1) && die "Invalid Parameters. Exiting";
open I, "< $ARGV[0]";

Hi Balajesuri.
Thank you for your informations.
Best regards.