COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### find the missing number......

• Email
• Follow

```Hello everybody!

I have a series of numbers like the following

703001
703002
703004
703005
703008
703010

I want to get the missing numbers. Here they are..

703003
703006
703007
703009

I wrote the following code to get the difference of the numbers but
failed to get the missing numbers...

awk '{ ar[NR]=\$0}END{
for (i=1; i<=NR; i++){ printf "%6d - %6d = %2d\n",ar[i+1],ar[i],ar[i
+1]-ar[i]}}' input

how to get the missing numbers.  Please gimmi an idea. Thank you.

```
 0

See related articles to this posting

```nag wrote:

> Hello everybody!
>
> I have a series of numbers like the following
>
> 703001
> 703002
> 703004
> 703005
> 703008
> 703010
>
> I want to get the missing numbers. Here they are..
>
> 703003
> 703006
> 703007
> 703009
>
> I wrote the following code to get the difference of the numbers but
> failed to get the missing numbers...
>
> awk '{ ar[NR]=\$0}END{
> for (i=1; i<=NR; i++){ printf "%6d - %6d = %2d\n",ar[i+1],ar[i],ar[i
> +1]-ar[i]}}' input
>
> how to get the missing numbers.  Please gimmi an idea. Thank you.

Assuming there are no missing numbers before the first entry and after the
last...

awk '(\$0 - p) > 1 && NR>1{for(i=p+1;i<\$0;i++)print i} {p = \$0}'
```
 0

```On 3/12/2010 9:36 AM, nag wrote:
> Hello everybody!
>
> I have a series of numbers like the following
>
> 703001
> 703002
> 703004
> 703005
> 703008
> 703010
>
> I want to get the missing numbers. Here they are..
>
> 703003
> 703006
> 703007
> 703009
>
> I wrote the following code to get the difference of the numbers but
> failed to get the missing numbers...
>
> awk '{ ar[NR]=\$0}END{
> for (i=1; i<=NR; i++){ printf "%6d - %6d = %2d\n",ar[i+1],ar[i],ar[i
> +1]-ar[i]}}' input
>
> how to get the missing numbers.  Please gimmi an idea. Thank you.
>

awk 'p{for (i=++p;i<\$0;i++) print i}{p=\$0}' file

Ed.
```
 0

```"Ed Morton" <mortonspam@gmail.com> wrote in message
news:hndopu\$rr4\$1@news.eternal-september.org...
> On 3/12/2010 9:36 AM, nag wrote:
>> Hello everybody!
>>
>> I have a series of numbers like the following
>>
>> 703001
>> 703002
>> 703004
>> 703005
>> 703008
>> 703010
>>
>> I want to get the missing numbers. Here they are..
>>
>> 703003
>> 703006
>> 703007
>> 703009
>>
>> I wrote the following code to get the difference of the numbers but
>> failed to get the missing numbers...
>>
>> awk '{ ar[NR]=\$0}END{
>> for (i=1; i<=NR; i++){ printf "%6d - %6d = %2d\n",ar[i+1],ar[i],ar[i
>> +1]-ar[i]}}' input
>>
>> how to get the missing numbers.  Please gimmi an idea. Thank you.
>>
>
> awk 'p{for (i=++p;i<\$0;i++) print i}{p=\$0}' file
>
>    Ed.

Aw, that's mostly my idea! Just for the sake of variation:

awk 'NR>1{while (++p < \$0) print p-1}{p=\$0}' file

...although I think it may actually work a little bit harder (3 vs. 2 ops in
main loop).

- Anton Treuenfels

```
 0

```Fri, 12 Mar 2010 18:29:11 -0600, Anton Treuenfels did cat :

> "Ed Morton" <mortonspam@gmail.com> wrote in message
> news:hndopu\$rr4\$1@news.eternal-september.org...
>> On 3/12/2010 9:36 AM, nag wrote:
>>> Hello everybody!
>>>
>>> I have a series of numbers like the following
>>>
>>> 703001
>>> 703002
>>> 703004
>>> 703005
>>> 703008
>>> 703010
>>>
>>> I want to get the missing numbers. Here they are..
>>>
>>> 703003
>>> 703006
>>> 703007
>>> 703009
>>>
>>> I wrote the following code to get the difference of the numbers but
>>> failed to get the missing numbers...
>>>
>>> awk '{ ar[NR]=\$0}END{
>>> for (i=1; i<=NR; i++){ printf "%6d - %6d = %2d\n",ar[i+1],ar[i],ar[i
>>> +1]-ar[i]}}' input
>>>
>>> how to get the missing numbers.  Please gimmi an idea. Thank you.
>>>
>>>
>> awk 'p{for (i=++p;i<\$0;i++) print i}{p=\$0}' file
>>
>>    Ed.
>
> Aw, that's mostly my idea! Just for the sake of variation:
>
> awk 'NR>1{while (++p < \$0) print p-1}{p=\$0}' file
>
> ..although I think it may actually work a little bit harder (3 vs. 2 ops
> in main loop).
>
> - Anton Treuenfels

or, for the sake of variety and golf ;-)
\$ awk 'n{while(\$0-++n)print n}{n=\$0}' file
```
 0

```Loki Harfagr escribió:
> Fri, 12 Mar 2010 18:29:11 -0600, Anton Treuenfels did cat :
>
>> "Ed Morton" <mortonspam@gmail.com> wrote in message
>> news:hndopu\$rr4\$1@news.eternal-september.org...
>>> On 3/12/2010 9:36 AM, nag wrote:
>>>> Hello everybody!
>>>>
>>>> I have a series of numbers like the following
>>>>
>>>> 703001
>>>> 703002
>>>> 703004
>>>> 703005
>>>> 703008
>>>> 703010
>>>>
>>>> I want to get the missing numbers. Here they are..
>>>>
>>>> 703003
>>>> 703006
>>>> 703007
>>>> 703009
>>>>
>>>> I wrote the following code to get the difference of the numbers but
>>>> failed to get the missing numbers...
>>>>
>>>> awk '{ ar[NR]=\$0}END{
>>>> for (i=1; i<=NR; i++){ printf "%6d - %6d = %2d\n",ar[i+1],ar[i],ar[i
>>>> +1]-ar[i]}}' input
>>>>
>>>> how to get the missing numbers.  Please gimmi an idea. Thank you.
>>>>
>>>>
>>> awk 'p{for (i=++p;i<\$0;i++) print i}{p=\$0}' file
>>>
>>>    Ed.
>> Aw, that's mostly my idea! Just for the sake of variation:
>>
>> awk 'NR>1{while (++p < \$0) print p-1}{p=\$0}' file
>>
>> ..although I think it may actually work a little bit harder (3 vs. 2 ops
>> in main loop).
>>
>> - Anton Treuenfels
>
>  or, for the sake of variety and golf ;-)
> \$ awk 'n{while(\$0-++n)print n}{n=\$0}' file

Or, to avoid repeating the last assignment for each record:

gawk "BEGIN{getline n} n{while(\$0-++n)print n}" file

--
```
 0

```Manuel Collado escribió:
> Loki Harfagr escribió:
>> Fri, 12 Mar 2010 18:29:11 -0600, Anton Treuenfels did cat :
>>
>>> "Ed Morton" <mortonspam@gmail.com> wrote in message
>>> news:hndopu\$rr4\$1@news.eternal-september.org...
>>>> On 3/12/2010 9:36 AM, nag wrote:
>>>>> Hello everybody!
>>>>>
>>>>> I have a series of numbers like the following
>>>>>
>>>>> 703001
>>>>> 703002
>>>>> 703004
>>>>> 703005
>>>>> 703008
>>>>> 703010
>>>>>
>>>>> I want to get the missing numbers. Here they are..
>>>>>
>>>>> 703003
>>>>> 703006
>>>>> 703007
>>>>> 703009
>>>>>
>>>>> I wrote the following code to get the difference of the numbers but
>>>>> failed to get the missing numbers...
>>>>>
>>>>> awk '{ ar[NR]=\$0}END{
>>>>> for (i=1; i<=NR; i++){ printf "%6d - %6d = %2d\n",ar[i+1],ar[i],ar[i
>>>>> +1]-ar[i]}}' input
>>>>>
>>>>> how to get the missing numbers.  Please gimmi an idea. Thank you.
>>>>>
>>>>>
>>>> awk 'p{for (i=++p;i<\$0;i++) print i}{p=\$0}' file
>>>>
>>>>    Ed.
>>> Aw, that's mostly my idea! Just for the sake of variation:
>>>
>>> awk 'NR>1{while (++p < \$0) print p-1}{p=\$0}' file
>>>
>>> ..although I think it may actually work a little bit harder (3 vs. 2 ops
>>> in main loop).
>>>
>>> - Anton Treuenfels
>>
>>  or, for the sake of variety and golf ;-)
>> \$ awk 'n{while(\$0-++n)print n}{n=\$0}' file
>
> Or, to avoid repeating the last assignment for each record:
>
> gawk "BEGIN{getline n} n{while(\$0-++n)print n}" file
>

Oops! The 'n' pattern now becomes redundant:

gawk "BEGIN{getline n} {while(\$0-++n)print n}" file

--
```
 0

```Mon, 15 Mar 2010 12:29:07 +0100, Manuel Collado did cat :

>> Loki Harfagr escribió:
>>> Fri, 12 Mar 2010 18:29:11 -0600, Anton Treuenfels did cat :
>>>
>>>> "Ed Morton" <mortonspam@gmail.com> wrote in message
>>>> news:hndopu\$rr4\$1@news.eternal-september.org...
>>>>> On 3/12/2010 9:36 AM, nag wrote:
>>>>>> Hello everybody!
>>>>>>
>>>>>> I have a series of numbers like the following
>>>>>>
>>>>>> 703001
>>>>>> 703002
>>>>>> 703004
>>>>>> 703005
>>>>>> 703008
>>>>>> 703010
>>>>>>
>>>>>> I want to get the missing numbers. Here they are..
>>>>>>
>>>>>> 703003
>>>>>> 703006
>>>>>> 703007
>>>>>> 703009
>>>>>>
>>>>>> I wrote the following code to get the difference of the numbers but
>>>>>> failed to get the missing numbers...
>>>>>>
>>>>>> awk '{ ar[NR]=\$0}END{
>>>>>> for (i=1; i<=NR; i++){ printf "%6d - %6d =
>>>>>> %2d\n",ar[i+1],ar[i],ar[i +1]-ar[i]}}' input
>>>>>>
>>>>>> how to get the missing numbers.  Please gimmi an idea. Thank you.
>>>>>>
>>>>>>
>>>>> awk 'p{for (i=++p;i<\$0;i++) print i}{p=\$0}' file
>>>>>
>>>>>    Ed.
>>>> Aw, that's mostly my idea! Just for the sake of variation:
>>>>
>>>> awk 'NR>1{while (++p < \$0) print p-1}{p=\$0}' file
>>>>
>>>> ..although I think it may actually work a little bit harder (3 vs. 2
>>>> ops in main loop).
>>>>
>>>> - Anton Treuenfels
>>>
>>>  or, for the sake of variety and golf ;-)
>>> \$ awk 'n{while(\$0-++n)print n}{n=\$0}' file
>>
>> Or, to avoid repeating the last assignment for each record:
>>
>> gawk "BEGIN{getline n} n{while(\$0-++n)print n}" file
>>
>>
> Oops! The 'n' pattern now becomes redundant:
>
>   gawk "BEGIN{getline n} {while(\$0-++n)print n}" file

or, a bit "golfier" and "look ma, no getline" ;-)
\$ awk '!n&&n=\$0{next}{while(\$0-++n)print n}' file
```
 0

```On 3/15/2010 9:37 AM, Loki Harfagr wrote:
> Mon, 15 Mar 2010 12:29:07 +0100, Manuel Collado did cat :
>
>>> Loki Harfagr escribió:
>>>> Fri, 12 Mar 2010 18:29:11 -0600, Anton Treuenfels did cat :
>>>>
>>>>> "Ed Morton"<mortonspam@gmail.com>  wrote in message
>>>>> news:hndopu\$rr4\$1@news.eternal-september.org...
>>>>>> On 3/12/2010 9:36 AM, nag wrote:
>>>>>>> Hello everybody!
>>>>>>>
>>>>>>> I have a series of numbers like the following
>>>>>>>
>>>>>>> 703001
>>>>>>> 703002
>>>>>>> 703004
>>>>>>> 703005
>>>>>>> 703008
>>>>>>> 703010
>>>>>>>
>>>>>>> I want to get the missing numbers. Here they are..
>>>>>>>
>>>>>>> 703003
>>>>>>> 703006
>>>>>>> 703007
>>>>>>> 703009
>>>>>>>
>>>>>>> I wrote the following code to get the difference of the numbers but
>>>>>>> failed to get the missing numbers...
>>>>>>>
>>>>>>> awk '{ ar[NR]=\$0}END{
>>>>>>> for (i=1; i<=NR; i++){ printf "%6d - %6d =
>>>>>>> %2d\n",ar[i+1],ar[i],ar[i +1]-ar[i]}}' input
>>>>>>>
>>>>>>> how to get the missing numbers.  Please gimmi an idea. Thank you.
>>>>>>>
>>>>>>>
>>>>>> awk 'p{for (i=++p;i<\$0;i++) print i}{p=\$0}' file
>>>>>>
>>>>>>     Ed.
>>>>> Aw, that's mostly my idea! Just for the sake of variation:
>>>>>
>>>>> awk 'NR>1{while (++p<  \$0) print p-1}{p=\$0}' file
>>>>>
>>>>> ..although I think it may actually work a little bit harder (3 vs. 2
>>>>> ops in main loop).
>>>>>
>>>>> - Anton Treuenfels
>>>>
>>>>   or, for the sake of variety and golf ;-)
>>>> \$ awk 'n{while(\$0-++n)print n}{n=\$0}' file
>>>
>>> Or, to avoid repeating the last assignment for each record:
>>>
>>> gawk "BEGIN{getline n} n{while(\$0-++n)print n}" file
>>>
>>>
>> Oops! The 'n' pattern now becomes redundant:
>>
>>    gawk "BEGIN{getline n} {while(\$0-++n)print n}" file
>
> or, a bit "golfier" and "look ma, no getline" ;-)
> \$ awk '!n&&n=\$0{next}{while(\$0-++n)print n}' file

You can save a character, I think:

awk '!n{n=\$0;next}{while(\$0-++n)print n}' file

Fore!

Ed.
```
 0

```Ed Morton wrote:

>> or, a bit "golfier" and "look ma, no getline" ;-)
>> \$ awk '!n&&n=\$0{next}{while(\$0-++n)print n}' file
>
> You can save a character, I think:
>
>      awk '!n{n=\$0;next}{while(\$0-++n)print n}' file

If you return to the good old "n=\$0", you can save even more:

awk 'n{for(;\$0>++n;)print n}{n=\$0}' file

```
 0

```pk wrote:
> Ed Morton wrote:
>
>>> or, a bit "golfier" and "look ma, no getline" ;-)
>>> \$ awk '!n&&n=\$0{next}{while(\$0-++n)print n}' file
>> You can save a character, I think:
>>
>>      awk '!n{n=\$0;next}{while(\$0-++n)print n}' file
>
> If you return to the good old "n=\$0", you can save even more:
>
>        awk 'n{for(;\$0>++n;)print n}{n=\$0}' file
>

Not introducing anything essential new and also not saving any
characters WRT golf, but isn't this one easier to understand?

awk 'n{while(\$0>++n)print n}{n=\$0}' file

Janis
```
 0

```On 3/15/2010 12:19 PM, Janis Papanagnou wrote:
> pk wrote:
>> Ed Morton wrote:
>>
>>>> or, a bit "golfier" and "look ma, no getline" ;-)
>>>> \$ awk '!n&&n=\$0{next}{while(\$0-++n)print n}' file
>>> You can save a character, I think:
>>>
>>> awk '!n{n=\$0;next}{while(\$0-++n)print n}' file
>>
>> If you return to the good old "n=\$0", you can save even more:
>>
>> awk 'n{for(;\$0>++n;)print n}{n=\$0}' file
>>
>
> Not introducing anything essential new and also not saving any
> characters WRT golf, but isn't this one easier to understand?
>
> awk 'n{while(\$0>++n)print n}{n=\$0}' file

Yes and it's interesting how many iterations it took to get from (just changed
"p" to "n" in the original to highlight the only important difference of getting
rid of the 2nd variable, "i"):

awk 'n{for(i=++n;i<\$0;i++)print i}{n=\$0}' file
to:
awk 'n{while(\$0>++n)print n}{n=\$0}' file

Cheers,

Ed.
```
 0

```Mon, 15 Mar 2010 18:19:55 +0100, Janis Papanagnou did cat :

> pk wrote:
>> Ed Morton wrote:
>>
>>>> or, a bit "golfier" and "look ma, no getline" ;-) \$ awk
>>>> '!n&&n=\$0{next}{while(\$0-++n)print n}' file
>>> You can save a character, I think:
>>>
>>>      awk '!n{n=\$0;next}{while(\$0-++n)print n}' file
>>
>> If you return to the good old "n=\$0", you can save even more:
>>
>>        awk 'n{for(;\$0>++n;)print n}{n=\$0}' file
>>
>>
> Not introducing anything essential new and also not saving any
> characters WRT golf, but isn't this one easier to understand?
>
>           awk 'n{while(\$0>++n)print n}{n=\$0}' file
>
>
> Janis

er, isn't that we're now almost back to where I started that damned hole number six :D)
\$ awk 'n{while(\$0>++n)print n}{n=\$0}' file

AAMOF the fork by Manuel Collado was essentially
"to avoid repeating the last assignment for each record:"

point that these three ways simply miss:
awk 'n{while(\$0-++n)print n}{n=\$0}' file
awk 'n{for(;\$0>++n;)print n}{n=\$0}' file
awk 'n{while(\$0>++n)print n}{n=\$0}' file

too much post trimming, pk, bunker! ;-)
```
 0

```Mon, 15 Mar 2010 12:36:16 -0500, Ed Morton did cat :

> On 3/15/2010 12:19 PM, Janis Papanagnou wrote:
>> pk wrote:
>>> Ed Morton wrote:
>>>
>>>>> or, a bit "golfier" and "look ma, no getline" ;-) \$ awk
>>>>> '!n&&n=\$0{next}{while(\$0-++n)print n}' file
>>>> You can save a character, I think:
>>>>
>>>> awk '!n{n=\$0;next}{while(\$0-++n)print n}' file
>>>
>>> If you return to the good old "n=\$0", you can save even more:
>>>
>>> awk 'n{for(;\$0>++n;)print n}{n=\$0}' file
>>>
>>>
>> Not introducing anything essential new and also not saving any
>> characters WRT golf, but isn't this one easier to understand?
>>
>> awk 'n{while(\$0>++n)print n}{n=\$0}' file
>
> Yes and it's interesting how many iterations it took to get from (just
> changed "p" to "n" in the original to highlight the only important
> difference of getting rid of the 2nd variable, "i"):

that's right, and hat's only because I've been too much trained to
use 'n' for members of space 'N' :D)
When I saw your first solution I only said "gee, nice!" then later on
thought that it was a pity that the use of 'while' was so many times ignored
in favor of the more expressive and readable 'for' then I thought (that was the
day I was supposed to take thinking chores ,-) that it could be a golf case,

>
>      awk 'n{for(i=++n;i<\$0;i++)print i}{n=\$0}' file
> to:
>      awk 'n{while(\$0>++n)print n}{n=\$0}' file

And, anyway I prefer your final victory here:-)

>> \$ awk '!n&&n=\$0{next}{while(\$0-++n)print n}' file
>>
>You can save a character, I think:
>
>   awk '!n{n=\$0;next}{while(\$0-++n)print n}' file
```
 0

```On 3/15/2010 12:54 PM, Loki Harfagr wrote:
> Mon, 15 Mar 2010 12:36:16 -0500, Ed Morton did cat :
>
>> On 3/15/2010 12:19 PM, Janis Papanagnou wrote:
>>> pk wrote:
>>>> Ed Morton wrote:
>>>>
>>>>>> or, a bit "golfier" and "look ma, no getline" ;-) \$ awk
>>>>>> '!n&&n=\$0{next}{while(\$0-++n)print n}' file
>>>>> You can save a character, I think:
>>>>>
>>>>> awk '!n{n=\$0;next}{while(\$0-++n)print n}' file
>>>>
>>>> If you return to the good old "n=\$0", you can save even more:
>>>>
>>>> awk 'n{for(;\$0>++n;)print n}{n=\$0}' file
>>>>
>>>>
>>> Not introducing anything essential new and also not saving any
>>> characters WRT golf, but isn't this one easier to understand?
>>>
>>> awk 'n{while(\$0>++n)print n}{n=\$0}' file
>>
>> Yes and it's interesting how many iterations it took to get from (just
>> changed "p" to "n" in the original to highlight the only important
>> difference of getting rid of the 2nd variable, "i"):
>
> that's right, and hat's only because I've been too much trained to
> use 'n' for members of space 'N' :D)
> When I saw your first solution I only said "gee, nice!" then later on
> thought that it was a pity that the use of 'while' was so many times ignored
> in favor of the more expressive and readable 'for' then I thought (that was the
> day I was supposed to take thinking chores ,-) that it could be a golf case,
>
>>
>>       awk 'n{for(i=++n;i<\$0;i++)print i}{n=\$0}' file
>> to:
>>       awk 'n{while(\$0>++n)print n}{n=\$0}' file
>
> And, anyway I prefer your final victory here:-)

that wasn't mine, it was janis's

Ed.

>
>>> \$ awk '!n&&n=\$0{next}{while(\$0-++n)print n}' file
>>>
>> You can save a character, I think:
>>
>>    awk '!n{n=\$0;next}{while(\$0-++n)print n}' file

```
 0

```Mon, 15 Mar 2010 15:08:03 -0500, Ed Morton did cat :

> On 3/15/2010 12:54 PM, Loki Harfagr wrote:
>> Mon, 15 Mar 2010 12:36:16 -0500, Ed Morton did cat :
>>
>>> On 3/15/2010 12:19 PM, Janis Papanagnou wrote:
>>>> pk wrote:
>>>>> Ed Morton wrote:
>>>>>
>>>>>>> or, a bit "golfier" and "look ma, no getline" ;-) \$ awk
>>>>>>> '!n&&n=\$0{next}{while(\$0-++n)print n}' file
>>>>>> You can save a character, I think:
>>>>>>
>>>>>> awk '!n{n=\$0;next}{while(\$0-++n)print n}' file
>>>>>
>>>>> If you return to the good old "n=\$0", you can save even more:
>>>>>
>>>>> awk 'n{for(;\$0>++n;)print n}{n=\$0}' file
>>>>>
>>>>>
>>>> Not introducing anything essential new and also not saving any
>>>> characters WRT golf, but isn't this one easier to understand?
>>>>
>>>> awk 'n{while(\$0>++n)print n}{n=\$0}' file
>>>
>>> Yes and it's interesting how many iterations it took to get from (just
>>> changed "p" to "n" in the original to highlight the only important
>>> difference of getting rid of the 2nd variable, "i"):
>>
>> that's right, and hat's only because I've been too much trained to use
>> 'n' for members of space 'N' :D)
>> When I saw your first solution I only said "gee, nice!" then later on
>> thought that it was a pity that the use of 'while' was so many times
>> ignored in favor of the more expressive and readable 'for' then I
>> thought (that was the day I was supposed to take thinking chores ,-)
>> that it could be a golf case,
>>
>>
>>>       awk 'n{for(i=++n;i<\$0;i++)print i}{n=\$0}' file
>>> to:
>>>       awk 'n{while(\$0>++n)print n}{n=\$0}' file
>>
>> And, anyway I prefer your final victory here:-)
>
> that wasn't mine, it was janis's
>
> 	Ed.

Oh, I'm not sure now as the thread has been severely 'GGfied[TM(R)(c)]' by now
but what I meant was my fav' was that one (re)quoted below which I believe was yours:?

>>>> \$ awk '!n&&n=\$0{next}{while(\$0-++n)print n}' file
>>> You can save a character, I think:
>>>    awk '!n{n=\$0;next}{while(\$0-++n)print n}' file
```
 0

```Loki Harfagr wrote:
> Mon, 15 Mar 2010 15:08:03 -0500, Ed Morton did cat :
>
>>> And, anyway I prefer your final victory here:-)
>> that wasn't mine, it was janis's
>>
>> 	Ed.
>
> Oh, I'm not sure now as the thread has been severely 'GGfied[TM(R)(c)]' by now
> but what I meant was my fav' was that one (re)quoted below which I believe was yours:?

I certainly don't claim any rights. :-)

Janis
```
 0