I have a file named file1 which contains numbers in sequence like...
1
2
3
7
8
Then i have file File 2 that contains
4
5
........so i need to compare both files so that i can find out the missing entry in the sequence is 6.......These files are flat files which contain and so i need to write a script that takes as input these 2 files.
I guess i cant use diff command or comm command for this , if i can how can i do it plz let me know guys.
I get this result:
kcsdev:/home/jmcnama> cat file1
1
2
3
4
5
7
8
kcsdev:/home/jmcnama> cat file2
9
10
kcsdev:/home/jmcnama> s.sh
6 is missing
from this code:
#!/bin/ksh
# script = s.sh
let expected=1
for i in $( cat file1 file2 | sort -n -u)
do
if [[ $expected != $i ]] ; then
echo " $expected is missing"
break
fi
let expected=$expected+1
done
hey thanks for the reply......but i dont understand how u can get the missing nos from the sequence in both the files.............if i have 2 files like file1 containing
1
2
5
and then file2...
3
then i need to find out that 4 is the missing no in the sequence from both the files.........
#!/bin/ksh
# script = s.sh
let expected=1
for i in $( cat file1 file2 | sort -n -u)
do
if [[ $expected != $i ]] ; then
echo " $expected is missing"
expected=$i
fi
let expected=$expected+1
done
This reports multiple missing elements. The example you gave above only was missing "6". If you need to find which file is missing what nubmer, use grep -q on the original files to locate the file that is missing the number.
Deleting the post
$ cat 11
1
2
3
7
8
$ cat 22
4
5
11
$ cat 11 22 | sort -n -u | ./a
6
9
10
$ cat a
#!/bin/awk -f
BEGIN {
i = 1;
}
/^[0-9]+/ {
while (i < $1) {
print i;
i++;
}
i++;
}