ksh/awk help - output missing numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh/awk help - output missing numbers
# 1  
Old 12-16-2008
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
# 2  
Old 12-17-2008
input file:
Code:
[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.
Code:
[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:
Code:
[yogeshs@helptoldreal-lr temp]$ sh check.sh
num1=2
num2=5
num3=6
num4=9
num5=10
[yogeshs@helptoldreal-lr temp]$

# 3  
Old 12-18-2008
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:
Code:
#!/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:
Code:
% ./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
# 4  
Old 12-18-2008
Thanks for the suggestions!
# 5  
Old 12-18-2008
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?
# 6  
Old 12-18-2008
Try...
Code:
$ 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


Last edited by Ygor; 12-18-2008 at 08:24 PM..
# 7  
Old 12-19-2008
Code:
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[i]!=0)
	{
		n++
		print "num"n"="arr[i]
	}
}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with awk script to get missing numbers in column 1

Hello to all, I have show below a file separated by commas. In first column has numbers where the last number is 13. 1,4 2,6 3,7 5,2 6,5 7,5 8,65 9,10 11,78 13,2 What I want to know is which numbers are missing from 1 to 13 (in this case 13 is last number in column 1). My real... (17 Replies)
Discussion started by: Ophiuchus
17 Replies

2. Shell Programming and Scripting

ksh : need to store the output of a awk command to a array

I have awk command : awk -F ' ' '{ print $NF }' log filename And it gives the output as below: 06:00:00 parameters: SDS (2) no no no no doc=4000000000). information: (6 Replies)
Discussion started by: ramprabhum
6 Replies

3. Shell Programming and Scripting

Formatting output using awk in ksh

:oi was trying to write a script to format output of a command in ksh which has output as below: so i used : to get which i require at all times. But problem occurs when status part changes. above output i get when status is SU (success).If the status is IN (inactive), output of... (1 Reply)
Discussion started by: pr5439
1 Replies

4. UNIX for Dummies Questions & Answers

How to combine and insert missing consecutive numbers - awk or script?

Hi all, I have two (2) sets of files that are based on some snapshots of database that I want to merge and insert any missing sequential number. Below are example representation of these files: file1: DATE TIME COL1 COL2 COL3 COL4 ID 01/10/2013 0800 100 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

5. Shell Programming and Scripting

awk - output-data always two numbers

Hi, my data is like the subsequent snipped. Fieldseperator is TAB. I can work the data well with awk, but the missing zero-numbers at the days column, for the days smaller 10 and the full hour-minutes i cant handle in the output. 2012 7 1 8 40 249.463 245.01 5.70448 6.11388 6.22125... (2 Replies)
Discussion started by: IMPe
2 Replies

6. Shell Programming and Scripting

Fill missing numbers in second column with zeros

Hi All, I have 100 files with names like this: 1.dat, 2.dat, 3.dat until 100.dat. My dat files look like this: 42323 0 438939 1 434 0 0.9383 3434 120.23 3 234 As you can see in the second column, some numbers are missing. I want to fill those missing places with 0's in all... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

7. UNIX for Dummies Questions & Answers

Help required on Printing of Numbers, which are missing in the range

Hi Experts, Need help on printing of numbers, which are missing in the range. Pls find the details below Input 1000000002 1000000007 1234007940 1234007946 Output 1000000003 1000000004 1000000005 1000000006 1234007941 (2 Replies)
Discussion started by: krao
2 Replies

8. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

9. Shell Programming and Scripting

Fill in missing numbers in range

I need to edit a list of numbers on the following form: 1 1.0 2 1.4 5 2.1 7 1.9 I want: 1 1.0 2 1.4 3 0.0 4 0.0 5 2.1 6 0.0 7 1.9 (i want to add the missing number in column 1 together with 0.0 in column 2). I guess it is rather trivial but i didn't even manage to read column... (5 Replies)
Discussion started by: bistru
5 Replies
Login or Register to Ask a Question