
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
答え2
短縮されたawk
スクリプトは、あなたの例とまったく同じ入力形式を期待します(最初の行の9番目のフィールドに含まれる数字を、合計行数から2を引いた数字と比較します)。
< in awk 'NR==1 {c=$9} END {if (c==FNR-2) print "ok"; else print "ko"}'