0

I have a big list of ordered files with names like this

file_1.txt
file_2.txt
file_3.txt
file_6.txt
file_7.txt
file_8.txt
file_10.txt

In this case it is easy to see that files: file_4.txt,file_5.txt and file_9.txt are missing, but if i have a big list how can i find the missing files? i am just learning bash so i just know some simple examples. like this

for i in $(seq 1 1000) ;

do  
  if [i not in *.txt];  then                                                                                                     
  echo $i;

done

But this doesnt even work unless i erase the if [i not in *.txt];then line so it just writes all the numbers between 1 and 1000. I hope you can help me. Thanks in advance.

0aslam0
  • 1,797
  • 5
  • 25
  • 42

3 Answers3

1

If they are in a file then this should work

awk 'match($0,/([0-9]+)/,a){a[1]>max&&max=a[1];b[a[1]]++}
     END{for(i=1;i<max;i++)if(!b[i])print "file_"i".txt"}' file

Output

file_4.txt
file_5.txt
file_9.txt
Community
  • 1
  • 1
1

The suggestion from @user4453924 really helped me out. It does not have to be in a file, just pipe output from ls into his awk command, and you should be fine:

ls *.txt | awk 'match($0,/([0-9]+)/,a){a[1]>max&&max=a[1];b[a[1]]++}
 END{for(i=1;i<max;i++)if(!b[i])print "file_"i".txt"}'

Outputs:

file_4.txt
file_5.txt
file_9.txt

Alternatively, if you prefer to do it in a two step fashion, it would be quite simple to pipe the output from ls into a file, and then use his command directly on the file, as it is:

ls *.txt > filelist.txt
awk 'match($0,/([0-9]+)/,a){a[1]>max&&max=a[1];b[a[1]]++}
 END{for(i=1;i<max;i++)if(!b[i])print "file_"i".txt"}' filelist.txt
Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
audunsol
  • 398
  • 3
  • 9
0

One way to do this is by

## TODO: You need to change the following path:
THELIST=/path/to/input-file

for i in $(seq 1 10); 
do 
  FOUND=`grep "file_$i.txt" "$THELIST"` #look for file $i in $THELIST
                                        #Note: double quotes were placed around $THELIST 
                                        #      in case there is whitespace in the filename 

  [[ "$FOUND" == "" ]] && echo $i     #if what you found is empty, then output $i
done

Community
  • 1
  • 1
csiu
  • 3,159
  • 2
  • 24
  • 26