2

I am new to Unix Shell scripting. I need to write a shell script for comparing 2 sql output files (basically in .txt format) which are the outputs of the same sql query (before and after a maintenance task like cloning) and print the differences into another text file.

Output file 1 (before clone):

NAME      OPEN_MODE 
--------- ----------
PROD123   READ WRITE

Output file 2 (after clone):

NAME      OPEN_MODE 
--------- ----------
DEV123    READ WRITE

We need to compare the above two output files and print the difference into another text file based on the differences. For example:

Here in the above output, The "NAME" column value doesn't match. So the difference should be printed into another file as:

NAME is not matching. OPEN_MODE is matching. Please check.

And the output file will be having multiple such outputs. So, we also need to check all those outputs and spool the difference to another file. Any sample shell script to achieve the same would be helpful.

Regards, AR

  • @Moshe. Thanks for the response. But "diff" is giving the output in the below format: < PROD123 READ WRITE --- > DEV123 READ WRITE and I'm looking for the below format: NAME is not matching. Please check. Regards, AR – user206245 Dec 19 '16 at 14:24
  • Do you just need to know which lines changed, or do you need to know which elements of which lines changed? If it's the latter you will need to write a script in a language like Python, Perl, or Ruby that can parse this kind of file. Then you can just iterate row by row and print out the differences. – shadowtalker Dec 19 '16 at 14:33
  • Also are the two files always the same length? What happens when one is longer than the other? – shadowtalker Dec 19 '16 at 14:35
  • @ssdecontrol - We need to know which elements of which lines changed and capture that comparison in to a different file. – user206245 Dec 19 '16 at 14:55
  • In that case you need to write a script in a more powerful programming language than the shell. Depending on your data you might be able to use AWK for it, but there's no reason not to use a "bigger" language here. – shadowtalker Dec 19 '16 at 15:08
1

This is a solution using Awk:

#!/usr/bin/awk -f

NR == 1 {
    # Get the headers on the first line.
    # Will be done for both files, but we don't mind.
    head[1] = $1;
    head[2] = $2;
}

NR == 3 {
    # For the third line, go though the data in both columns...
    for (i = 1; i <= 2; ++i) {
        # If there's something in col[], then this is the second file.
        if (col[i]) {
            # This is the second file.
            # Test the value of the column against what was in the first file.
            if (col[i] == $i) {
                printf("%s is matching. ", head[i]);
            } else {
                printf("%s is not matching. ", head[i]);
                # Flag that we need to print extra info later.
                check = 1;
            }
        } else {
            # This is the first file.
            # Remember the data in the two columns.
            col[1] = $1;
            col[2] = $2;

            # Reset the record (line) counter.
            NR = 0;

            # Skip to the second file.
            nextfile;
        }
    }

    if (check) {
        printf("Please check.");
    }

    printf("\n");
}

Testing it:

$ ./script.awk file1 file2
NAME is not matching. OPEN_MODE is matching. Please check.
0

using diff

diff file1 file2 > changes.log

  • Thanks for your response. But "diff" doesn't give the output in the format that I am looking for. "diff" gave the output as: < PROD123 READ WRITE --- > DEV123 READ WRITE and I am looking for the output as "NAME is not PROD123. Please check." – user206245 Dec 19 '16 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.