Validate file count in korn shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Validate file count in korn shell script
# 1  
Old 03-15-2013
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.

Code:
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
Code:
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.

Last edited by Scrutinizer; 03-15-2013 at 06:34 AM.. Reason: code tags
# 2  
Old 03-15-2013
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
Code:
$ ls *.txt | awk -F"[ _.]" '$6!=LAST {MAX[$6]=$5;LAST=$6} {MAX[$6]--} END {for (i in MAX) EC+=MAX[i]; exit EC!=0}'

If you can make sure that there's never MORE files than max no., this will do:
Code:
ls *.txt | awk -F"[ _.]" '!MAX[$6] {MAX[$6]=$5} {MAX[$6]--} END {for (i in MAX) EC+=MAX[i]; exit EC!=0}'

# 3  
Old 03-15-2013
@RudiC: if you use plain ls and filter within awk for *.txt then your suggestion will not be susceptible to maximum line length.

Last edited by Scrutinizer; 03-15-2013 at 07:21 AM..
# 4  
Old 03-15-2013
@Scrutinizer: Not sure I understand what you imply...
# 5  
Old 03-15-2013
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.
# 6  
Old 03-15-2013
Ahhh - got you. Of course. I was thinking on an entirely different path... Yes, could be easily implemented.
# 7  
Old 03-17-2013
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.

Code:
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...
Code:
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

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

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

Code like..
Code:
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

Moderator's Comments:
Mod Comment Also use code tags for pseudo code

Last edited by Scrutinizer; 03-18-2013 at 03:15 AM.. Reason: code tags also for pseudo code..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help Needed with File Checker Korn Shell Script

I am attempting to write a korn shell script that does the following, but I am getting errors. I'm new to korn shell scripting and am not sure what I am doing wrong. Please help with example scripts. Thanks. 1) check for day of the week 2) if day of the week is Monday then check for the... (4 Replies)
Discussion started by: ijmoore
4 Replies

2. Shell Programming and Scripting

Help with korn shell script to get the latest file versions in a directory

I want to write a korn shell script to get the latest three versions for a file in the directory having lot of files with various versions (files with prefix as same but time stamp as suffix) and compress it and at the same time have to remove the remaining versions of the file (other than latest... (4 Replies)
Discussion started by: maheshbabu
4 Replies

3. Shell Programming and Scripting

Need a ready Shell script to validate a high volume data file

Hi, I am looking for a ready shell script that can help in loading and validating a high volume (around 4 GB) .Dat file . The data in the file has to be validated at each of its column, like the data constraint on each of the data type on each of its 60 columns and also a few other constraints... (2 Replies)
Discussion started by: Guruprasad
2 Replies

4. Programming

create a spool file based on values passed from korn shell to sql script

this is my issue. 4 parameters are passed from korn shell to sql script. parameter_1= varchar2 datatype or no value entered my user. parameter_2= number datatype or no value entered my user. parameter_3= number datatype or no value entered my user. parameter_4= number datatype or no... (5 Replies)
Discussion started by: megha2525
5 Replies

5. Shell Programming and Scripting

Write output to a file using Korn shell script

All, Can anyone please help me with the below scenario in korn shell script. Can anyone please give me some hints to proceed on this. I have a Flat file of the below format. Input file format:... (1 Reply)
Discussion started by: sp999
1 Replies

6. Shell Programming and Scripting

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each certificate and send the mail to the user. The user takes action to add the new certificate to the storage file and user owns the responsibility to update the input text file with the new certificate... (5 Replies)
Discussion started by: casmo
5 Replies

7. Shell Programming and Scripting

Monitor file generation for an hour using Korn shell script

Hi, I am new to this unix scripting, I got a requirement like.. Files with *.XML extension will be generating in a /home/sample/ folder for every 15 mins. I need to monitor those files in that particular folder for every hour. If no file has been generated in that particular folder for an... (7 Replies)
Discussion started by: siri_886
7 Replies

8. Shell Programming and Scripting

shell script to get the arrival count of file

Hello All, I have come across a small problem. It would be great if any of you could help me in resolving the issue. one file named dummy.txt will be ftped to Unix machine twice daily. If i receive it second time in a day i need to do some processing with the file. How to find the... (2 Replies)
Discussion started by: RSC1985
2 Replies

9. UNIX for Dummies Questions & Answers

korn shell script to keep history of same file

Hello, How do I write a korn shell that will rename file with the current date. What I want to do is, I have a file that is re-written every day. But instead, I want to keep a 14 day history of the file. So I want to write a script that will rename the current file with a date attached to the... (2 Replies)
Discussion started by: watson2000
2 Replies

10. Shell Programming and Scripting

Korn Shell Script - Read File & Search On Values

I am attempting to itterate through a file that has multiple lines and for each one read the entire line and use the value then to search in other files. The problem is that instead of an entire line I am getting each word in the file set as the value I am searching for. For example in File 1... (2 Replies)
Discussion started by: run_unx_novice
2 Replies
Login or Register to Ask a Question