ヘッダーのレコード数の確認

ヘッダーのレコード数の確認

Record_count合計詳細レコードがヘッダーレコードと同じであることを確認し、そうでない場合はエラーを発生させるスクリプトを作成しようとします。

サンプル

0001  HD  SAP _AP Distribution  20150615  131723  000003  00000003   

detail record 1
detail record 2
detail record 3

タイトル00000003はレコード数です。

ベストアンサー1

使用awk

awk '! /^detail/ && /.+/ {max=$9} /^detail record/ {count++} END {if (max == count) { print "ok, "max" = "count} else { print "not ok, "max" != "count }}' foo

またはbashスクリプトで

#!/bin/bash
retValue=$(awk '! /^detail/ && /.+/ {max=$9} /^detail record/ {count++} END {if (max != count) { print "1" }}' "$1")

if [[ "$retValue" -eq 1 ]]; then
    exit 1
fi

exit 0

起動スクリプト:

<script_name> <data_file>

はい

% cat foo                                                                    
0001  HD  SAP _AP Distribution  20150615  131723  000003  00000003   

detail record 1
detail record 2
detail record 3

% awk '! /^detail/ && /.+/ {max=$9} /^detail record/ {count++} END {if (max == count) { print "ok, "max" = "count} else { print "not ok, "max" != "count }}' foo
ok, 00000003 = 3

% cat bar
0001  HD  SAP _AP Distribution  20150615  131723  000003  00000004   

detail record 1
detail record 2
detail record 3

% awk '! /^detail/ && /.+/ {max=$9} /^detail record/ {count++} END {if (max == count) { print "ok, "max" = "count} else { print "not ok, "max" != "count }}' bar
not ok, 00000004 != 3

おすすめ記事