Validate file count in korn shell script

Hi,

I have files in the directory like below which I need to validate if all the required files are present.

A_B_001 of 002_time1.txt
A_B_002 of 002_time1.txt
A_B_001 of 001_time2.txt

Scenarios-
a)If file with 001 of 002_time1 or 002 of 002_time1 is missing in the folder,script should fail.Script should pass only if 2 files are present
b)At the same time if there is any other file with 001 of 001 or 001of 002,002 of 002 with different timestamp,script should pass.
Eg:In the above case script should pass since all the files are there
But if the files are like below,script should fail since 002 0f 002_time2 is missing

A_B_001 of 002_time1.txt
A_B_002 of 002_time1.txt
A_B_001 of 002_time2.txt

File count for each timestamp can vary i.e.it may be 001 of 001 on one day and 3 files on another.

When I extract the 2nd part of the count in the filenamei.e.002 and compare with the total count,the code works if there are only 2 files.But if there is an additional file with 001 of 001 or 001 of 002,002 of 002 process fails.

Pls help.

Thanks in advance.

This specification is absolutely unreadable! If I guess its meaning correctly, you have a number of files per timestamp, with the file number, the maximum file number, and the timestamp being part of the file name. You want to check if the count of files per timestamp is identical to the maximum file number. For this, try

$ ls *.txt | awk -F"[ _.]" '$6!=LAST {MAX[$6]=$5;LAST=$6} {MAX[$6]--} END {for (i in MAX) EC+=MAX; exit EC!=0}'

If you can make sure that there's never MORE files than max no., this will do:

ls *.txt | awk -F"[ _.]" '!MAX[$6] {MAX[$6]=$5} {MAX[$6]--} END {for (i in MAX) EC+=MAX; exit EC!=0}' 

@RudiC: if you use plain ls and filter within awk for *.txt then your suggestion will not be susceptible to maximum line length.

@Scrutinizer: Not sure I understand what you imply...

ls *.txt will fail if there are too many .txt files in the directory, whereas ls will never fail. You could read all the files provided by the plain ls and only process those that end with .txt .

Ahhh - got you. Of course. I was thinking on an entirely different path... Yes, could be easily implemented.

Thanks for the help..I have tried to explain better this time..
Now my reqmt suddenly changed..I need to use a looping mechanism t check if all the files are available ignoring the timestamp.

A_B_001of002_time1.txt
A_B_002of002_time1.txt
A_B_001of001_time2.txt

I just need to ignore time1,time2 basically timestamp.I need to perform this for all the files in the directory matching the file mask(A_B_*.txt)

Considering 1 set of files,I need a loop to check if all the files for that particular set are available..
like...

first time...curr=001,max=002
if curr eq max
  print"all files found"
else 
   print "file is missing"
   continue #read the next file
fi

Output:file is missing

second time..curr=002,max=002
Output:all files found

third time..curr=001,max=002
Output:all files found

Code like..

file_format=A_B_*.txt
ls $file_format|sort -u
curr=`echo $file_format |awk -F_ '{ print substr($3,3,1)}'`
max=`echo $file_format |awk -F_ '{ print substr($3,8,1)}'`
for file in `ls $file_format`
do
<Looping>
done

I need help on the looping logic..Pls help ..

Thanks