|
Unire i file con AWK di filtraggio e di conteggio delle linee
Hi there,
Ho un paio di file ho bisogno di unione. Non posso fare un semplice da concatenare fondere in un unico grande file.
Ma poi ho bisogno di filtrare i file per ottenere un risultato desiderato.
L'output è simile a questa:
Codice:
TRNH 0000000010941
ORDH
OADR
OADR
ORDL
ENDT 1116399 000000003 000000001
TRLR 0000000010941 000000003 000000001
TRNH 0000000010942
ORDH
OADR
OADR
ORDL
ENDT 1116400 000000003 000000001
TRLR 0000000010942 000000003 000000001
TRNH 0000000010943
ORDH
OADR
OMSG
ORDL
ORDL
ENDT 1116399 000000004 000000001
TRLR 0000000010943 000000003 000000001
TRNH 0000000010944
ORDH
OADR
OADR
ORDL
ENDT 1116400 000000003 000000001
ORDH
OADR
OADR
ORDL
ORDL
ENDT 1116400 000000004 000000001
TRLR 0000000010944 000000007 000000002
e il filtro dovrebbe lasciare la prima e l'ultima linea con la TRNH e TRLR (dando l'ultimo TRLR la stessa sequenza come il primo TRNH). Il resto del TRNH e TRLR linee devono essere omessi.
Poi il finale TRLR dovrebbe rappresentare la quantità di linee ORDH e l'importo della OADR, OMSG e ORDL linee.
Non ho ottenuto lo scopo di sopprimere l'extra TRNH e linee TRLR ancora, questo è il filtro che ho finora:
Codice:
BEGIN {
# define two counters
ordh_cnt = 0;
ordl_total_cnt = 0;
}
# Start filter
# if line start with ORDH add 1 to counters
$1 == "ORDH" {
ordh_cnt++;
}
# if line starts with TRLR, adjust line to reflect new count of ORDH in order
$1 == "TRLR" {
printf "%s%9.9d%s\n", substr($0, 0, 31), ordh_cnt, substr($0, 39);
# line has been printed, next rule
next;
}
# if line start with ORDL add 1 to counters
$1 == "ORDL" {
ordl_total_cnt++;
}
# if line start with OADR add 1 to counters
$1 == "OADR" {
ordl_total_cnt++;
}
# if line start with OMSG add 1 to counters
$1 == "OMSG" {
ordl_total_cnt++;
}
# if line starts with TRLR, adjust line to reflect new total ORDL, OADR and OMSG in complete file
$1 == "TRLR" {
printf "%s%9.9d%s\n", substr($0, 0, 19), ordl_total_cnt, substr($0, 29);
# line has been printed, next rule
next;
}
# Line has not changed, print normal line
{
print $0;
}
Ora la quantità di linee ORDH è uscita al mio nuovo file, in modo che sembra funzionare. Tuttavia, la quantità di OADR, OMSG e ORDL linee non è stato corretto in uscita.
Il risultato finale dovrebbe essere simile al seguente:
Codice:
TRNH 0000000010941
ORDH
OADR
OADR
ORDL
ENDT 1116399 000000003 000000001
ORDH
OADR
OADR
ORDL
ENDT 1116400 000000003 000000001
ORDH
OADR
OMSG
ORDL
ORDL
ENDT 1116399 000000004 000000001
ORDH
OADR
OADR
ORDL
ENDT 1116400 000000003 000000001
ORDH
OADR
OADR
ORDL
ORDL
ENDT 1116400 000000004 000000001
TRLR 0000000010941 000000017 000000005
Qualsiasi aiuto sarebbe molto apprezzato
|