Сравните количество строк файла unix с количеством трейлеров для нескольких типов записей

Вы также читали примеры на qnx man mount?

mount -e

This will re-read the disk partition table for /dev/hd0, and create, update or delete /dev/hd0tXX block-special files for each partition.

С вашей командой ssh ​​это имеет смысл. понимаю не много.

0
03.05.2021, 13:11
3 ответа
#!/usr/bin/awk -f

/^000[0-9]/ {
    i=substr($1,1,4);
    sub(/^0+/,"",i);
    bc[i]++;   # bc = body count array
    rtotal++;
}

/^T999/ {
  dc = substr($0,5,3);
  for (r in bc) {
    rc[r] = substr($0,(r-1)*3+8,3); # rc = row count array
    sub(/^0+/,"",rc[r]);
    ttotal+=rc[r]
  };
}

END {
  if (dc == rtotal) {
    printf "Overall detail row count %i matches with trailer record count %i.\n", rtotal, dc;
  } else {
     print "Row count %i does not match trailer record count %i", rtotal, dc;
     exit 1;
  }
  for (r in rc) {
    if (rc[r] == bc[r]) {
      printf "Row count for %04i record type %i matches with trailer record count %i.\n", r, bc[r], rc[r];
    } else {
      printf "Row count for %04i record type %i does not match with trailer record count %i.\n", r, bc[r], rc[r];
      print "Stopping execution"
      exit 2;
    };
  };
}

Предполагается, что завершающая строка T999будет иметь 3 символа для счетчика данных (dc )И для каждой строки массива bc.

Если в данных есть какие-либо ошибки, он завершится либо с несоответствием 1 (общего количества строк и итогового количества данных ), либо с несоответствием 2 (общего количества строк и итоговой записи ).. Это можно протестировать и использовать, например, в операторе case оболочки с построенной оболочкой -в переменной$?(код выхода для последней команды):

 #!/bin/sh

./summarise.awk file.txt
 ec="$?"
 case "$ec" in
    0) probably_do_nothing ;; # success! 0 == no error
    1) do_something ;;
    2) do_another_thing ;;
 esac

Сохранить как, например. summarise.awkи сделайте исполняемый файл с помощью chmod +x summarise.awk.

$./summarise.awk file.txt 
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
0
28.07.2021, 11:35

Использование awk:

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
1
28.07.2021, 11:35
$ cat tst.awk
/^[0-9]/ {
    currType = substr($0,1,4)
    if ( currType != prevType ) {
        recNr2type[++actNumRecs] = currType
        prevType = currType
    }
    recNrs2rowCnts[actNumRecs]++
    actTotRows++
    next
}
{ trailer = $0 }
END {
    expTotRows = substr(trailer,5,3)+0

    result = (actTotRows == expTotRows ? "matches" : "does not match")
    printf "Overall detail row count %d %s with trailer record count %d.\n", \
        actTotRows, result, expTotRows

    trailer = substr(trailer,8)
    while ( (trailer != "") && (result == "matches") ) {
        expRowCnt = substr(trailer,1,3)+0
        actRowCnt = recNrs2rowCnts[++expNumRecs]
        type      = recNr2type[expNumRecs]

        result = (actRowCnt == expRowCnt ? "matches" : "does not match")
        printf "Row count for %s record type %s %s with trailer record count %d.\n", \
            actRowCnt, type, result, expRowCnt

        trailer = substr(trailer,4)
    }

    print "Stopping execution."
}

$ awk -f tst.awk file
Overall detail row count 8 matches with trailer record count 8.
Row count for 2 record type 0001 matches with trailer record count 2.
Row count for 2 record type 0002 matches with trailer record count 2.
Row count for 3 record type 0003 does not match with trailer record count 4.
Stopping execution.
0
28.07.2021, 11:35

Теги

Похожие вопросы