Tuesday, March 8, 2016

Use Python to compare files (Use case: Rename Oracle redo-logs in asymmetric data guard setup or ASM)

Data Files:



Output: 


Source Code:

import sys
import csv
import os.path

def convert(sourcecsv, targetcsv):
    if not os.path.isfile(sourcecsv):
        print ("File [%s] is not invalid. Quiting..." % (sourcecsv))
        return
    if not os.path.isfile(targetcsv):
        print ("File [%s] is not invalid. Quiting..." % (targetcsv))
        return
    sourcelist = []
    targetlist = []
    validlist = []
    with open(sourcecsv,'r') as f:
        reader = csv.reader(f, delimiter = ',')
        for row in reader:
            if len(row) > 0 :
                sourcelist.append([int(row[0].strip()),row[1].strip()])

    with open(targetcsv,'r') as f:
        reader = csv.reader(f, delimiter = ',')
        for row in reader:
            if len(row) > 0 :
                targetlist.append([int(row[0].strip()),row[1].strip()])

    # source the list based on group#    sourcelist.sort()
    targetlist.sort()

    for (gid1,member1) in sourcelist:
        for (gid2, member2) in targetlist:
            if gid1 == gid2:
                 if member1[1] == ':':
                     if member1[0:1] == member2[0:1]:
                         validlist.append([gid1,member1,member2])
                 else:
                     if member1[1:].split('/')[0] == member2[1:].split('/')[0]:
                         validlist.append([gid1,member1,member2])


    for (gid,member1,member2) in validlist:
        print("alter database rename file '%s' to '%s';" % (member1,member2))
        sourcelist.remove([gid,member1])
        targetlist.remove([gid,member2])

    print('\n')
    for (gid,member1) in sourcelist:
        print("Redo log without match in source file: {0:d}, {1}".format(gid,member1))

    print('\n')
    for (gid,member2) in targetlist:
        print("Redo log without match in target file: {0:d}, {1}".format(gid,member2))

if __name__ == "__main__":
   if len(sys.argv) != 3:
       print("Usage: python %s source_redo_file.csv target_redo_file.csv" % (sys.argv[0]))
       print("These CSV files in format:group#,member")
       print("Reference SQL: select group#||','||member from v$logfile order by group#")
       exit(1)
   convert(sys.argv[1],sys.argv[2])

No comments:

Post a Comment