I need to write a shell script that pick all the files (not directories) in /exp/files directory. For each file inside the directory I want to find whether the last line of file is received . The last line in the file is a trailer record. Also the third field in the last line is the number of data records count i.e 2315 (Total Number of lines in the file -2 (header,trailer) ) . In my unix shell script i want to check whether the last line is a trailer record by checking T and want to check whether the number of lines in the file is equal to (2315+2). If this is successful then i want to move the file to a different directory /exp/ready.
tail -1 test.csv
T,Test.csv,2315,80045.96
Also in the inputfile sometimes 0 or 1 more fields of trailer record can be within double quotes
"T","Test.csv","2315","80045.96"
"T", Test.csv, 2212,"80045.96"
T,Test.csv,2315,80045.96
You can test for the presence of the last line with the following:
At that point
$rcwill be 0 if the line started with eitherT,or"T",, assuming that’s enough to catch the trailer record.Once you’ve established that, you can extract the line count with:
and you can get the expected line count with:
and compare the two.
So, tying all that together, this would be a good start. It outputs the file itself (my test files
num[1-9].tst) along with a message indicating whether the file is okay or why it is not okay.The sample run, showing the test files I used: