2つのSQL出力ファイルを列名と比較し、違いを別のファイルに印刷します。

2つのSQL出力ファイルを列名と比較し、違いを別のファイルに印刷します。

私はUnixシェルスクリプトに初めて触れました。同じSQLクエリ(複製などのメンテナンス操作の前後)の出力である2つのSQL出力ファイル(デフォルトでは.txt形式)を比較し、異なるテキストファイルに違いを印刷するシェルスクリプトを作成する必要があります。

出力ファイル1(複製前):

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

出力ファイル2(複製後):

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

上記の2つの出力ファイルを比較し、違いに基づいて他のテキストファイルに違いを印刷する必要があります。たとえば、

上記の出力では、「NAME」列の値が一致しません。したがって、違いは次のように別のファイルに印刷する必要があります。

名前が一致しません。 OPEN_MODE が一致します。ご確認ください。

そして、出力ファイルにはこれらの出力がいくつかあります。したがって、私たちはこれらすべての出力を確認し、違いを別のファイルにスプールする必要があります。同じ目的を達成するすべてのサンプルシェルスクリプトが役に立ちます。

ありがとう、アーカンソー

ベストアンサー1

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");
}

テストしてみてください:

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

おすすめ記事