You need to enable JavaScript to run this app. Unix多类型记录文件明细行数与Trailer计数校验实现咨询
优惠活动
大模型
产品
解决方案
定价
更多

Unix多类型记录文件明细行数与Trailer计数校验实现咨询

完整校验脚本实现

原有半成品脚本存在两处常见问题:

  • 匹配规则错误:原正则^00[01-25]的字符类仅能匹配0、1、2、5开头的行,无法精准定位目标明细记录
  • 字段截取偏移错误:bash字符串索引从0开始,原脚本对trailer字段的截取偏移量多1位,会取错计数段的值

可直接运行的完整脚本

#!/bin/bash
# 传入待校验文件路径作为参数
filename="$1"

# 校验文件是否存在
if [[ ! -f "$filename" ]]; then
    echo "Target file $filename does not exist."
    echo "Stopping execution."
    exit 1
fi

# 提取trailer行
trailer=$(grep "^T999" "$filename" | head -n1)
if [[ -z "$trailer" ]]; then
    echo "No valid T999 trailer record found in file."
    echo "Stopping execution."
    exit 1
fi

# 按1-based位置规则转换为bash 0-based偏移,提取trailer中各计数(自动去前导零)
total_trailer=$((${trailer:4:3} + 0))
c0001_trailer=$((${trailer:7:3} + 0))
c0002_trailer=$((${trailer:10:3} + 0))
c0003_trailer=$((${trailer:13:3} + 0))
c0004_trailer=$((${trailer:16:3} + 0))

# 统计文件内实际各类型明细行数
total_actual=$(grep -Ec '^(0001|0002|0003|0004)' "$filename")
c0001_actual=$(grep -cE '^0001' "$filename")
c0002_actual=$(grep -cE '^0002' "$filename")
c0003_actual=$(grep -cE '^0003' "$filename")
c0004_actual=$(grep -cE '^0004' "$filename")

# 第一步:校验总明细计数
if [[ $total_actual -ne $total_trailer ]]; then
    echo "Overall detail row count $total_actual does not match with trailer record count $total_trailer."
    echo "Stopping execution."
    exit 1
fi
echo "Overall detail row count $total_actual matches with trailer record count $total_trailer."

# 逐类型校验
## 校验0001类型计数
if [[ $c0001_actual -ne $c0001_trailer ]]; then
    echo "Row count for 0001 record type $c0001_actual does not match with trailer record count $c0001_trailer."
    echo "Stopping execution."
    exit 1
fi
echo "Row count for 0001 record type $c0001_actual matches with trailer record count $c0001_trailer."

## 校验0002类型计数
if [[ $c0002_actual -ne $c0002_trailer ]]; then
    echo "Row count for 0002 record type $c0002_actual does not match with trailer record count $c0002_trailer."
    echo "Stopping execution."
    exit 1
fi
echo "Row count for 0002 record type $c0002_actual matches with trailer record count $c0002_trailer."

## 校验0003类型计数
if [[ $c0003_actual -ne $c0003_trailer ]]; then
    echo "Row count for 0003 record type $c0003_actual does not match with trailer record count $c0003_trailer."
    echo "Stopping execution."
    exit 1
fi
echo "Row count for 0003 record type $c0003_actual matches with trailer record count $c0003_trailer."

## 校验0004类型计数
if [[ $c0004_actual -ne $c0004_trailer ]]; then
    echo "Row count for 0004 record type $c0004_actual does not match with trailer record count $c0004_trailer."
    echo "Stopping execution."
    exit 1
fi
echo "Row count for 0004 record type $c0004_actual matches with trailer record count $c0004_trailer."

脚本说明

  • 运行方式:给脚本加执行权限后,传入待校验文件路径即可执行,例如./check.sh sample.txt
  • 计数处理:通过整数算术运算自动去除计数段的前导零,不需要额外调用sed处理
  • 终止逻辑:任意一步校验不匹配会直接通过exit 1终止流程,完全符合需求
  • 输出格式:所有匹配、不匹配的输出文案和要求的示例格式完全一致,对示例文件运行后会直接输出预期的中断校验结果

内容的提问来源于stack exchange,提问作者Mini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 12:54:07