Comparing Data file with Crtl file

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

Ex -
1,abc,1234
2,bcd,4567
3,cde,8901

and File B has details of File A

Ex-
FILEA.TXT|06/17|2010|3
(filename)|(received date)|(record count)

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 

It throws error at
Proc_dt=$(stat -c %y $fname | awk -F'[- ]' '{print $2 "/" $3 "/" $1}'

saying ( expected;
and

else
     echo "\n \n Process date doesnot match" 

saying else unexpected

Proc_dt=$(stat -c %y $fname | awk -F'[- ]' '{print $2 "/" $3 "/" $1}')

Add closing ) character.

Well you also have a non-numeric value in variable: Proc_dt because of following statement:

Proc_dt=$(stat -c %y $fname | awk -F'[- ]' '{print $2 "/" $3 "/" $1}')

And you are trying to perform a numeric comparison. I would also suggest to replace the if statement syntax to:

if [ $linecount -eq $rec_cnt ] && [ $Proc_dt -eq $Process_dt ] 

Yes, the following is wrong, as yoda noted:

if [ $linecount -eq $rec_cnt && $Proc_dt -eq $Process_dt ]  ; then

The other way to correct (one test, two conditions required) is:

if [ $linecount -eq $rec_cnt -a $Proc_dt -eq $Process_dt ]; then

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 I'm interpreting your question correctly, this is perfectly correct:

if [ $x -eq 1 ]; then

So is this:

if [ $x -eq 1 ]
  then

Thanks a lot hanson44

I understood and it worked :slight_smile:

but the console is not going inside the while loop

while IFS='|' && read fname Process_dt rec_cnt
do 
set of statements
done < POR.crtl

it has to print filename procdate and record count but none is executing..!
is ter any problem in while statement ?

Lose the "&&" operator

while IFS='|' read fname Process_dt rec_cnt

Would this work? Don't you just need to set IFS once?

IFS='|'
while read fname Process_dt rec_cnt
  do 
  set of statements
done < POR.crtl

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 ??

Try making a test case to get this part working:

$ cat test.txt
file_1|process_1|cnt_1
file_2|process_2|cnt_2
$ 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
1 Like

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

sorry I just gone out of mind :confused:
that was a different topic and I posted it wrongly, thanks for your concern and you have helped me in framing out the code. :):b:

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 ?

file A
abcd
efgh

file B

ijkl
mnop

output must be -

file C
abcd
efgh
ijkl
mnop

The best way is:

cat fileA fileB > fileC
1 Like