Hi,
I need to compare a file with its contents matching to that of another file(filename , received date and record count).
Lets say has File A original data
I need to fetch the details(filename , received date and record count) File A ,and must be compared with File B and it should match.I wrote a code which covers all the requirements but it throws me an error. It throws error at line 62 stating ']' missing and line 72 : argument expected. Kindly have a look at my script and let me know where I went wrong.
Original Script
echo "Initialising the run time parameter for FOLDER path..."
cd $path #path will be given here
echo "\n Folder path of the unzipped files and CNTRL file: $1 "
echo "\n \n Reading the content from CNTRL file and asssigning the values to variables..."
while IFS='|' && read fname Process_dt rec_cnt
do
echo "\n $fname $Process_dt $rec_cnt "
echo "\n Checking if the File exist in the folder..."
if [ -f $fname ] ; then
echo "\n $fname Exist"
echo "\n \n Reading linecount for the corresponding file and storing it in a variable..."
cat $fname | wc -l | read linecount
echo "\n Reading Processdate for the corresponding file and storing it in a variable..."
ls -l | awk '{print $6 $7 S8}'| nawk ' { months=" JanFebMarAprMayJunJulAugSepOctNovDec";date=$2;month=index(months,substr($1,1,3))/3; year=$3; printf("%02s/%02s/%s
\n",month,date,year)}'|read Proc_dt;
echo "\n checking if record count and Process date of the file matches with the CNTRL file entry..."
if [ ($linecount -eq $rec_cnt) && ($Proc_dt -eq $Process_dt) ] ; then
echo "\n \n Record Count and Process date matches from both files"
return 0
else
if [ ($linecount -ne $rec_cnt)]
echo "\n \n Record Count doesnot match"
return 1
fi
else
echo "\n \n Process date doesnot match"
return 1
fi
fi
echo "Passing the run time parameter for CNTRL file name..."
done < POR.CRTl
echo " Filename,Record count and Process date validated against file $2"
I will be incorporating this into a cmd task where I will pass path and CRTL file name as variable.
echo "Initialising the run time parameter for FOLDER path..."
cd $path #path will be given here
echo "\n Folder path of the unzipped files and CNTRL file: $1 "
echo "\n \n Reading the content from CNTRL file and asssigning the values to variables..."
while IFS='|' && read fname Process_dt rec_cnt
do
echo "\n $fname $Process_dt $rec_cnt "
echo "\n Checking if the File exist in the folder..."
if [ -f $fname ] ; then
echo "\n $fname Exist"
echo "\n \n Reading linecount for the corresponding file and storing it in a variable..."
linecount=$(wc -l $fname | cut -d' ' -f1)
echo "\n Reading Processdate for the corresponding file and storing it in a variable..."
Proc_dt=$(stat -c %y $fname | awk -F'[- ]' '{print $2 "/" $3 "/" $1}'
echo "\n checking if record count and Process date of the file matches with the CNTRL file entry..."
if [ $linecount -eq $rec_cnt && $Proc_dt -eq $Process_dt ] ; then
echo "\n \n Record Count and Process date matches from both files"
return 0
else
if [ $linecount -ne $rec_cnt ] # A space was missing between rec_cnt and ]
echo "\n \n Record Count doesnot match"
return 1
fi
else
echo "\n \n Process date doesnot match"
return 1
fi
fi
echo "Passing the run time parameter for CNTRL file name..."
done < POR.CRTl
thanks for timely help..Im not getting that error now.
but should we use ; in IF statements...?? Im not sure about this. to fetch process date I used ls -l | awk '{print $6 $7 S8}'| nawk ' { months=" JanFebMarAprMayJunJulAugSepOctNovDec";date=$2;month=index(months,substr($1,1,3))/3; year=$3; printf("%02s/%02s/%s",month,date,year)}'|read Proc_dt;
wont it work?
Also my first echo statement is printing , my script is executing but only last echo is being executed.
If we remove the '&&' operator and suppose the crtl file has details of more than one data file will this code work?
I tried giving IFS outside while loop still the while statement not executing...!
IFS='|'
while read fname Process_dt rec_cnt
do
echo "\n $fname $Process_dt $rec_cnt "
echo "\n Checking if the File exist in the folder..."
if [-f $fname]
then
echo "\n $fname Exist"
else
echo "\n $fname Does not Exist"
fi
done < POR.crtl
Try this. Note especially if [ -f $fname ] instead of if [-f $fname]
IFS='|'
while read fname Process_dt rec_cnt; do
echo "$fname, $Process_dt, $rec_cnt "
echo "Checking if the File exist in the folder..."
if [ -f $fname ]; then
echo "$fname Exist"
else
echo "$fname Does not Exist"
fi
done < POR.crtl
I have tried all possible but the console doesnt goes into the while loop itself only if it goes it will check for file existence and it prints relevant message.
I m not sure whether I have missed something in while loop. Placed IFS outside while but still problem persists. Do we have any other statements that we can instead of while ??
$ cat test.sh
#!/bin/bash
IFS='|'
while read fname Process_dt rec_cnt; do
echo "$fname, $Process_dt, $rec_cnt "
echo "Checking if the File exist in the folder..."
if [ -f $fname ]; then
echo "$fname Exist"
else
echo "$fname Does not Exist"
fi
done < test.txt
$ ls file_?
file_1
$ ./test.sh
file_1, process_1, cnt_1
Checking if the File exist in the folder...
file_1 Exist
file_2, process_2, cnt_2
Checking if the File exist in the folder...
file_2 Does not Exist
I have 2 data files and 2 crtl files.
Say-
A.txt B.txt
C.crtl D.crtl
crtl file has filename|processdate|recordcount
the data in crtl files must be compared with all the txt files
and it should validated
I wrote code which works for 1 txt and 1 crtl, I dont know
how to write for 2 crtl files and multiple txt files.
I'm afraid I'm a little unclear what the additional requirement is. What if you start a new topic, with a different title, maybe a title like "Comparing Multiple Data files with Ctrl files", because it's a little different, and post the information in code tags? In other words, make two sample control files, a few text files, the best shot at the current script you are using, and what output you are expecting. It can get confusing once a thread has gone on for a while, and we want everyone in a good mood.
sorry I just gone out of mind
that was a different topic and I posted it wrongly, thanks for your concern and you have helped me in framing out the code. :)
just one more help hanson44 -
how to concat two .crtl files and to write in separate .crtl file ?
can you help me with the syntax ?