how can I detect number series range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how can I detect number series range
# 1  
Old 12-04-2011
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.
kocaturk
# 2  
Old 12-04-2011
Try this...
Code:
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
# 3  
Old 12-04-2011
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
kocaturk
# 4  
Old 12-04-2011
input:
Code:
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

Code:
#! /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:
Code:
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

# 5  
Old 12-04-2011
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.
kocaturk
# 6  
Old 12-04-2011
Try this...
Code:
#!/bin/bash
input=$1
awk '{a[++j]=$2;b[j]=$1}
END{
  for(i=1;i<=j;i++){
   if(a[i+1]-a[i]==1 && b[i]==b[i+1]){
     a[i]==0?s="00":0;!s?s=a[i]:0;continue
   } print b[i],!s?a[i]:s,a[i];s=0 }
}' $input

Code:
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[i]==b[i+1]

--ahamed

Last edited by ahamed101; 12-04-2011 at 03:26 PM..
# 7  
Old 12-04-2011
Ahamed101 and Balajesuri,
thank you very much. last codes of Ahamed's are solved my problem.
thank you very much again.
kocaturk
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Detect continuous number as range

I have 100k data like this bellow , i want to group data to range 171 172 173 174 175 176 179 182 183 187 188 189 1900 1901 1903 1904 1905 1906 (10 Replies)
Discussion started by: before4
10 Replies

2. Shell Programming and Scripting

Generating Random Number in certain range

Hi there I am trying to generate a random number between 40 and 70 using the shell here is my code so far and it keeps going above 70. all help much appreciated! comp=$(( RANDOM%70+40 )) echo $comp (4 Replies)
Discussion started by: faintingquiche
4 Replies

3. Shell Programming and Scripting

Fill data if number range is given

Hi I want to get all numbers if number range is given as input. Eg: INPUT FILE 100-105 107 108-112 OUTPUT REQUIRED: 100 101 102 103 104 105 107 108 109 110 111 112 How can I do it using shell? :confused: Thanks in advance. (11 Replies)
Discussion started by: dashing201
11 Replies

4. Shell Programming and Scripting

If statement test against number range [0-9]

Is it possible to test against a varible within a ranges in a if statement. ex. if ];then echo "not in range" else echo "number within range" fi (8 Replies)
Discussion started by: leemalloy
8 Replies

5. Shell Programming and Scripting

Range of number from 0.1 to 10.0

Is there a way to create a loop that will output number starting from 0.1 to 10.0 0.1 0.2 0.3 0.4 0.5 .. ... 10.0 This is what i tried. for i in {1..50}; do printf -v i '%02d' $i ; echo "$i"; done That will print 01 02 03 .. .. 50 (9 Replies)
Discussion started by: vietrice
9 Replies

6. Shell Programming and Scripting

How to get the number series in between?

Hi Guys, Can someone give me a simple script that can extract the numbers in between numbers from start to end. As shown below, it start from 100 to 110 and revealed the numbers in between. INPUT: 100 - 110 DESIRED OUTPUT: 100 101 102 103 104 105 106 107 (6 Replies)
Discussion started by: pinpe
6 Replies

7. Shell Programming and Scripting

Number range for SSNs

Hi All. I have a file that has an ID Number field....some of the ID Numbers are actual SSNs. ...does anyone know the range that SSNs may be...this is what I have found so far poking around SSN info sites.... greater than 001-01-0000 and less than 770-00-0000. Does anyone know this to be... (1 Reply)
Discussion started by: lyoncc
1 Replies

8. UNIX for Dummies Questions & Answers

To find missing numbers from a number series

Hi, My requirement is I have an input file with a continuous series from 10000 to 99999. I have some numbers missing from those series. I want a output file which produces those missing numbers. Eg: 10002, 99999 are missing from the series then the output file should contain those... (4 Replies)
Discussion started by: rakeshbharadwaj
4 Replies

9. Shell Programming and Scripting

validate number range

Hi If I want to read user input and want to validate if it is a numeric number in some range of 1-100 what would be the best way? Sabina (5 Replies)
Discussion started by: sabina
5 Replies
Login or Register to Ask a Question