UNIXファイル行数を複数のタイプのレコード歌手と比較する

UNIXファイル行数を複数のタイプのレコード歌手と比較する

私のファイルには、複数のヘッダーと複数のレコードタイプ(例:0001、0002、0003、0004)があります。テール行の各レコードタイプの数は、完全な詳細レコードの数とともに提供されます。

サンプルファイル:

XYZH001
YZXH002
0001Rec1
0001Rec2
YZXH002
0002Rec1
0002Rec2
YZXH002
0003Rec1
0003Rec2
0003Rec3
YZXH002
0004Rec1
T999008002002004001

ファイルの詳細:

Detail records are where 1 to 4 position data in (0001, 0002, 0003, 0004)

Trailer:
Trailer identifier(position 1 to 4)             = T999
total data count (position 5 to 7)              = 008
count of record type 0001 (position 8 to 10)    = 002
count of record type 0002 (position 11 to 13)   = 002
count of record type 0003 (position 14 to 16)   = 004
count of record type 0004 (position 17 to 19)   = 001

必要:

-- Compare overall detail row count where 1 to 4 position data in (0001, 0002, 0003, 0004) with trailer record count (position 5 to 7) 
-- Compare each Record type row count with trailer record count
     eg. Compare row count where 1 to 4 position data = 0001 with trailer record count for 0001 (position 5 to 7) 
      .....
-- Stop execution in case of detail record row count and trailer count mismatch

予想出力:

Overall detail row count 8 matches with trailer record count 8.
Row count for 0001 record type 2 matches with trailer record count 2.
Row count for 0002 record type 2 matches with trailer record count 2.
Row count for 0003 record type 3 does not match with trailer record count 4.
Stopping execution.

ベストアンサー1

使用アッ:

awk '/^000[1-4]/{ rec[substr($0, 1, 4)+0]++; totalRec++ }
END{ trailTotalRec=substr($0, 5, 3)+0
     for(i=1; i<=4; i++) { trailRec[i]=substr($0, pos+8, 3)+0; pos+=3 }
    if(trailTotalRec==totalRec)
        print "Overall detail row count", totalRec, "matches with trailer record count", trailTotalRec"."
    for(i=1; i<=4; i++) {
        print "Row count for 000" i, "record type", rec[i], (trailRec[i]==rec[i]?"matches":"does not"), "with trailer record count", trailRec[i]"."
        if(trailRec[i]!=rec[i]) { print "Stopping execution."; exit 7 }
    }
}' infile

おすすめ記事