Find missed numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find missed numbers
# 1  
Old 02-28-2013
Find missed numbers

I have a file as follows:

1
3
7
12
78
...
999998
1000000

I want to find out all numbers not in the file above and put into another file like

2
4
5
6
8
9
10
11
13
...
999999

thank you
# 2  
Old 03-01-2013
One loop:
Code:
x=0 
c=0
 
while (( ++x <= 999999 ))
 do
  if (( x > c ))
   then
    read c
    if [ $? = 1 ]
     then
      c=1000000
     fi
   fi
  if (( x < c ))
   then
    echo $x
   fi
 done < infile

You need to check/generate all million numbers and then filter them with file numbers. Disposing of the edge cases tersely, never the same test twice, is important in getting a clean, minimal code. This ran almost 2 minutes for me. I could get smaller using the ternary operator '? :', but I am usually a ksh guy:
Code:
$ echo $(( 0 > 1 ? 2 : 3 ))
ksh:  0 > 1 ? 2 : 3 : syntax error
$ bash -c 'echo $(( 0 > 1 ? 2 : 3 ))'
3
$

Indentation indicates which line controls which. You, too, deserve properly indented code. It's an investment in your future.

If this is schoolwork, one needs to fill out a template.

Last edited by DGPickett; 03-01-2013 at 12:41 PM..
# 3  
Old 03-01-2013
What about
Code:
$ seq 1 1000000 | grep -vwf file

?
# 4  
Old 03-01-2013
Isn't that an unordered search? Try:
Code:
$ seq 1 999999 | comm -23 - file

# 5  
Old 03-01-2013
try also:
Code:
awk 'NR==1 {last=$1+1} {for (n=last; n<$1; n++) print n; last=$1+1}' infile

# 6  
Old 03-01-2013
Quote:
Originally Posted by DGPickett
Isn't that an unordered search? Try:
Code:
$ seq 1 999999 | comm -23 - file

comm obviously expects an alphanumerically ordered file, so 10 after 9 makes it complain.
grep -w looks for entire words, so 10 doesn't match 100, and it doesn't care for order. seq (I hope) guarantees the order of the numbers.
# 7  
Old 03-01-2013
Ah yes, need sorts:
Code:
comm -23 <( seq 1 999999 | sort ) <( sort file ) | sort -n

Less N x M. A numeric atof() option for comm might be nice.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script That Find Numbers Less Than 10

We have an existing script called "slots.sh" that prints a few numbers. I want to have another shell script that looks if there is any number from this output that is less than 10. If it does find a number that is less than 10, it then emails to me the output of "slot. sh", if it is equal to 10 or... (7 Replies)
Discussion started by: forextrafun
7 Replies

2. Programming

RegEx find numbers above 25000

Hello, I am using the Sublime Plugin LogHighlight. I can use RegEx there to highlight some lines in sublime. Now I need to find every line, that has a number of above 25000. the lines look like this: smart_sdl.result: 8947 smart_sdm.result: 8947 smart_sdn.result: 25000 Currently I am... (3 Replies)
Discussion started by: blend_in
3 Replies

3. Linux

Apache folder missed

Hi, I am confused on apache service. Please advice me There is no apache directory in my linux box. Even I cant find out httpd.conf file also. But If browse my Hostname in Internet Explor, I can able to apache service there. Please How it is working .. This file is available in... (1 Reply)
Discussion started by: Mani_apr08
1 Replies

4. Shell Programming and Scripting

AWK regex to find only numbers

Hi guys I need to find both negative and positive numbers from the following text file. And i also dont need 0. 0 8 -7 -2268 007 -07 -00 -0a0 0a0 -07a0 7a00 0a0 Can someone please give a regex to filter out the values in red. I tried a few things in awk but it didnt work... (9 Replies)
Discussion started by: sridanu
9 Replies

5. Shell Programming and Scripting

find the last word with all numbers?

Hi, I was trying to extract the last word with all numbers using awk. like the below example. I am looking for a pattern using awk. desired result: (13 Replies)
Discussion started by: hitmansilentass
13 Replies

6. HP-UX

vgchgid missed one disk

Hi, Suppose I have 3 disks c0t0d0, c0t0d1, and c0t0d2 all with the *same* VGID. I then run: # vgchgid /dev/rdsk/c0t0d0 /dev/rdsk/c0t0d1 (notice I *accidentally* skipped the third disk!) How would I fix this so that all 3 disks have the same VGID again? I'm looking for step-by-step... (7 Replies)
Discussion started by: apra143
7 Replies

7. Shell Programming and Scripting

to find numbers in a string

I writing my script and got stuck in this function. Can someone help me? I need to extract out the numbers inside a string. Ex: INPUT -> OUTPUT abcdef123 -> 123 abc123def -> 123 123abcdef -> 123 a123bc45d -> 123 45 abcdefghi -> -1 Thank you! (12 Replies)
Discussion started by: fongthai
12 Replies

8. Shell Programming and Scripting

to find numbers using awk

suppose u have a file 23 33 44 66 55 88 Another file 49 34 49 90 So to find where these numbers lie between in the first file output shud be while using second file and search for the first file 44 66 - 23 33 44 66 - (1 Reply)
Discussion started by: cdfd123
1 Replies

9. Shell Programming and Scripting

asked question about script before missed ansewr..

linux fedora core2 :) i am trying to write a script to clear, date, pwd and tty a linux termnal or konsole.. when I test the tty against $0 i am, getting a premission denied on the terminal that I am trying to printf to.. I tried using an awk command, test condition, an if then fi clause, but... (6 Replies)
Discussion started by: moxxx68
6 Replies
Login or Register to Ask a Question