ksh/awk help - output missing numbers

Here is what I am trying to do:

I have a list of numbers that I pulled from an awk command in a column like so:

1
3
4
7
8

I want to find which numbers in the list are missing out of a range. So let's say I want to find out from the list above which numbers are missing from the range of 1-10. The output should be:

2
5
6
9
10

I would also like each of the outputs stored in a string. So I would have something like this

num1=2
num2=5
num3=6
num4=9
num5=10

input file:

[yogeshs@helptoldreal-lr temp]$ cat newfile
1
3
4
7
8
[yogeshs@helptoldreal-lr temp]$

try this script. there could be a better way to do this.

[yogeshs@helptoldreal-lr temp]$ cat check.sh
counter=1
for i in 1 2 3 4 5 6 7 8 9 10
do
  grep $i newfile > /dev/null
  if [[ $? -ne 0 ]]
  then
    echo num$counter=$i
    let counter=$counter+1
  fi
done
[yogeshs@helptoldreal-lr temp]$

see the result:

[yogeshs@helptoldreal-lr temp]$ sh check.sh
num1=2
num2=5
num3=6
num4=9
num5=10
[yogeshs@helptoldreal-lr temp]$

Hi.

This example uses standard commands to address the sequence part of the problem. It finds the extrema of the input sequence, uses a command to generate the full sequence, then compares the sequences:

#!/usr/bin/env ksh

# @(#) s2       Demonstrate identification of missing items.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) seq comm
set -o nounset
echo

FILE=${1-data1}

rm -f t1 t2
echo " Data file $FILE:"
cat $FILE

echo
echo " Results:"
sort -n $FILE > t1
hi=$( tail -n 1 <t1 )
lo=$( head -n 1 <t1 )
n=$( expr $hi - $lo + 1 )
echo " lo, hi of set of $n is $lo, $hi"
seq $lo $hi > t2
# jot $n $lo $hi > t2
echo " Generated set is:"
cat t2

echo
echo " Missing items in sequence:"
comm -13 $FILE t2

exit 0

Producing:

% ./s2

(Versions displayed with local utility "version")
Linux 2.6.11-x1
pdksh 5.2.14 99/07/13.2
seq (coreutils) 5.2.1
comm (coreutils) 5.2.1

 Data file data1:
1
3
4
7
8

 Results:
 lo, hi of set of 8 is 1, 8
 Generated set is:
1
2
3
4
5
6
7
8

 Missing items in sequence:
2
5
6

The command jot can be used in place of seq ... cheers, drl

Thanks for the suggestions!

OK, I went with a slightly different approach.

#!/bin/ksh

query | awk '$1 == row1 {printf ""$2"\n"}' row1=$row1 > file1

comm -1 -3 file1 file2 > file3
#file2 is just a file with every value between 0 and 3000

awk 'NR == 1' file3
#I used this command to pull the values out of file3

So, what I really want to do is exactly the same as above, but without using any external files. Any suggestions?

Try...

$ cat file1
1
3
4
7
8

$ awk 'function f(a){while(++c<a)print c}{f($1)}END{f(d+1)}' d=10 file1
2
5
6
9
10

$ eval $(awk 'function f(a){while(++c<a)printf "num%d=%d\n",++b,c }{f($1)}END{f(d+1)}' d=10 file1)

$ echo $num1 $num2 $num3 $num4 $num5
2 5 6 9 10
awk 'BEGIN{
str="1,2,3,4,5,6,7,8,9,10"
n=0
split(str,arr,",")
}
{arr[$0]=0}
END{
  for (i in arr)
  	if(arr!=0)
	{
		n++
		print "num"n"="arr
	}
}' file