#!/bin/sh

# disable filename globbing
#

 BASE=`pwd`
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
export PATH APPLDIR DATADIR
agend=./tmp/agend

D1=./tmp/D1
tod=`date +%d/%m/%Y`
flag=$3
[ $flag = 0 ] && flag=""

# |  1 H00 regist 06 00   0 | 2 H01 pacien 05 51   6 | 3 H02 fechai 10 00  11 |
# |  4 H03 valida 04 00  21 | 5 H04 profes 12 52  25 | 6 H05 fechae 10 00  37 |
# |  7 H06 transs 01 00  47 | 8 H07 transc 20 00  48 | 9 H08 usuari 20 00  68 |
awk -F"^" ' BEGIN { inic=fec("'"$1"'"); fina=fec("'"$2"'");today=fec("'"$tod"'")  }
function fec(x) { return substr(x,7)""substr(x,4,2)""substr(x,1,2) }
FILENAME==ARGV[1] && fec($6) < inic {next}
FILENAME==ARGV[1] { r[substr($2,1,5)$4$6]++; r[substr($2,1,5)$4$6$7]=1; next}
r[$1$8$10]  != "'"$flag"'"  { next }
#{ print $1$8$10,r[$1$8$10]+0 } 
#r[$1$8$10]  != 0  { next} 
fec($10) > today { next } 
{ print } 
 ' $agend $D1.2 >$D1
exit
==> tmp/agend <==
000001^00001|HERRERA             ^26/11/2018^0300^17.142.186-1|Caroline Villena    ^26/12/2018^A^2019-03-25 20:22:09 ^Telemed2019         ^  ^  ^                                                                                                                                                                                                                                                                                                        ^
000002^00001|HERRERA             ^26/12/2018^0300^17.142.186-1|Caroline Villena    ^26/12/2018^V^2019-03-25 20:22:09 ^Telemed2019         ^  ^  ^                                                                                                                                                                                                                                                                                                        ^
000003^00001|HERRERA             ^26/11/2018^0300^17.142.186-1|Caroline Villena    ^26/12/2018^C^2019-03-25 20:22:09 ^Telemed2019         ^  ^  ^                                                                                                                                                                                                                                                                                                        ^

==> tmp/D1 <==
00145^JESSICA CARRERA^00054|Coronel^Psiclogo^04^Domicilio^4 ^0309^17/07/2020^23/07/2020^16/08/2020
00181^NELLY ZAMORA^00292|Iquique^Enfermera^02^Telemedicina^EX^0718^08/07/2020^23/07/2020^07/08/2020
00181^NELLY ZAMORA^00292|Iquique^Mdico^01^Domicilio^5 ^0018^08/07/2020^23/07/2020^07/08/2020
00210^JORGE RIQUELME^00147|Traigun^Curaciones^07^Domicilio^EX^0610^16/07/2020^23/07/2020^15/08/2020
00210^JORGE RIQUELME^00147|Traigun^Enfermera^02^Domicilio^5 ^0110^16/07/2020^23/07/2020^15/08/2020
00251^MAJIN TOLEDO^00183|Osorno^Enfermera^02^Domicilio^4 ^0105^21/07/2020^23/07/2020^20/08/2020
00291^SUSANA ROZAS^00169|Llanquihue^Mdico^01^Telemedicina^EX^0619^07/07/2020^23/07/2020^06/08/2020
00291^SUSANA ROZAS^00169|Llanquihue^Psiclogo^04^Domicilio^4 ^0319^07/07/2020^23/07/2020^06/08/2020
00312^MIGUEL OSSES^00053|Concepcin^Kinesilogo^03^Ambulatoria^4 ^0218^08/07/2020^23/07/2020^07/08/2020
00317^BARBARA ERLANDSEN^00003|Pichilemu^Enfermera^02^Domicilio^4 ^0118^08/07/2020^23/07/2020^07/08/2020
