Title: Shell script to find the missing numbers in a file
Tags: bash;shell
Question: Below is the example file I have with name ```Weeks```:
```2002
2004
2005
2006
2010
```
And I wanted a simple script to display the missing number from the files. I tried the below script but didn't get expected results.
```#!/bin/bash
filename='Weeks'
Sw=2001
while read line; do
  #Reading each line.
  if [ $line != $Sw ]
  then
    Sw=$(($Sw+1))
    echo &quot;$line&quot;
  else
    Sw=$(($Sw+1))
  fi
done < $filename
```
Expected output is:
```2001
2003
2007
2008
2009
```
Please help me I don't why the above script is not giving me correct output.

Comment: Why is 2001 expected, but not 2011 or 9999?


Here is another answer: ```comm -13 Weeks <(seq 2001 2010)
```
That should do the trick.
```seq```:
```NAME
       seq - print a sequence of numbers

SYNOPSIS
       seq [OPTION]... LAST
       seq [OPTION]... FIRST LAST
       seq [OPTION]... FIRST INCREMENT LAST

DESCRIPTION
       Print numbers from FIRST to LAST, in steps of INCREMENT.
       [...]
```
```comm```:
```NAME
       comm - compare two sorted files line by line

SYNOPSIS
       comm [OPTION]... FILE1 FILE2

DESCRIPTION
       Compare sorted files FILE1 and FILE2 line by line.

       When FILE1 or FILE2 (not both) is -, read standard input.

       With no options, produce three-column output.  Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files.

       -1     suppress column 1 (lines unique to FILE1)

       -2     suppress column 2 (lines unique to FILE2)

       -3     suppress column 3 (lines that appear in both files)
       [...]
```
The ```<(...)``` syntax tricks ```comm``` into believing, that the output of ```seq``` is actually a file.
In your original script there is an initial logic error, that follows execution until the end. Ask yourself, what happens in the very first iteration of the ```while``` loop: ```$Sw``` is ```2001```, ```$line``` is ```2002```. ```$Sw``` will be incremented to ```2002```, but in the next iteration compared to ```2004``` and so on.
Additionally you print ```$line```, which is the wrong variable.
A possible (albeit a bit inefficient) solution for the logic would be to nest another ```for``` loop in the while loop to look for and print items missing between the last ```$line``` value (that you need to store at the end of each loop in another variable) and the current ```$line``` value.

Comment for this answer: Hm, strange. If I do this on my machine, I get the exact result: https://i.imgur.com/kufxInT.png Are you sure, that the `Weeks` file is “clean”? That is, no whitespace at line ends or other hidden characters in it?
Comment for this answer: Hi Boldewyn, I tried this comm -13 weeks <(seq 2001 2010) for the weeks file as 
2002
2004
2005
2006
2010

and below is the O/P I got 
2001
2003
2004
2005
2006
2007
2008
2009

which is partially correct I didn't want 2004 2005 and 2006 also to be displayed.

And thanks for the correction on the script I will work on it.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:04/12/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<bash><shell>', 'Title': 'Shell script to find the missing numbers in a file', 'ViewCount': 470, 'id': 67056217, 'user_AboutMe': None, 'user_AccountId': 17104867.0, 'user_DisplayName': 'Musaveer STARk', 'user_Reputation': 17.0, 'user_Views': 9.0, 'user_id': 12377099.0}}