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
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]$
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