Script to compare substrings of multiple filenames and move to different directory

Hi there,

I am having trouble with a script I have written, which is designed to search through a directory for a header and payload file, retrieve a string from both filenames, compare this string and if it matches make a backup of the two files then move them to a different directory for processing. Example filenames are:

S202SCTWELADEDD20121119000662PACS008.xml
WELAIO_20121119000662PACS008.dat
WELADEDD_20121119150656_00000663CAMT056.xml
WELAIO_20121119000663CAMT056.dat
WELADEDD_20121119150650_00000664CAMT056.xml
WELAIO_20121119000664CAMT056.dat
 

I have one function which deals with the 'PACS' files - the xml is the payload and the .dat is the header. These always get moved as only one pair tends to appear at a time.

I have trouble with the CAMT files, as two pairs can appear at any one time. In order to minimise the risk of picking up the wrong files, I have been quite specific in my functions. The first function retrieves the CAMT payload file code:

GetCAMTCode ()
{
if [ $DEBUG -eq 1 ]
then
        set -xv
fi
cd $TRANSITDIR
LogMsg INFO "Checking CAM payload file codes"
ls *.xml | awk '{ print substr($1,18,9)}' | read camtcode

GrabAndMoveCAMT
}

For the examples above, this would return
150650_00
150656_00

I then run another function to retrieve the header code for the second half of the pair:

GrabAndMoveCAMT ()
{
if [ $DEBUG -eq 1 ]
then
        set -xv
fi
cd $TRANSITDIR
LogMsg INFO "Checking for CAMT files..."
for camstr in `ls *.dat | grep CAMT | awk '{ print substr($1,16,13)}'`
do
        if [[ -f WELAIO_${DATE}${camstr}.dat && -f ${PAYLOAD}${camtcode}${camstr}.xml ]]
        then
                LogMsg INFO "WELAIO_${DATE}${camstr}.dat and ${PAYLOAD}${camtcode}${camstr}.xml found in ${TRANSITDIR}"
                cp *${camstr}.* ${TRANSITDIR}/backup
                if [[ -f ${TRANSITDIR}/backup/WELAIO_${DATE}${camstr}.dat && -f ${TRANSITDIR}/backup/${PAYLOAD}${camtcode}${camstr}.xml ]]
                then
                        LogMsg INFO "WELAIO_${DATE}${camstr}.dat and ${PAYLOAD}${camtcode}${camstr}.xml copied to ${TRANSITDIR}/backup"
                        mv *${camstr}.* ${IMPORTDIR}
                        LogMsg INFO "WELAIO_${DATE}${camstr}.dat and ${PAYLOAD}${camtcode}${camstr}.xml moved to ${IMPORTDIR}"
                else
                        LogMsg FAIL "Backup copy fail WELAIO_${DATE}${camstr}.dat and ${PAYLOAD}${camtcode}${camstr}.xml"
                fi
        else
                LogMsg INFO "No files found"
        fi
done

This second function returns
000663CAMT056

The problem is that this compares the payload file WELADEDD_20121119150650_00000664CAMT056.xml with header file WELAIO_20121119000663CAMT056.dat and fails. I have tried using a substring from the end of the filename as a comparison, but because the rest of the filename differs I can't use filemasks in my comparison test (i.e. if [ -f *${camstr} ] translates to literally *000663CAMT056 instead of WELxxx00066 etc).

It has been suggested that I use an array. I have modified the first function like so:

set -A CAMTCODE `ls *.xml | awk '{ print substr($1,18,9)}'

`

This assigns the two strings correctly, but the problem appears when I do the comparison. If I look at my logs in debug mode, I see it is testing for the existence of these two files:

[[ -f WELADEDD_20121119150650_00 150656_00000663CAMT056.xml ]]

Which is incorrect.

If anyone can suggest to me how I can use the contents of an array to compare two strings in two different filenames, it would be much appreciated. Let me know if further information is required.

This is on korn shell, Solaris 10.

Al

Call GrabAndMoveCAMT function for each camtcode value:-

for xml_file in *.xml
do
   camtcode=$( echo $xml_file | cut -c 18-26 )
   GrabAndMoveCAMT
done
1 Like