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

perso=./tmp/perso.e
./appl/sh/perso | awk -F^ ' substr($16,1,2)=="00" { print $1"^"trail($2)"^"trail($1)"^"trail($12)"^"trail($10)"^"trail($9) } 
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) } ' | sort  >$perso

# |  1 H00 codigo 12 07   0 | 2 H01 identi 40 00  12 | 3 H02 nombre 40 00  52 |
# |  4 H03 patern 40 00  92 | 5 H04 matern 40 00 132 | 6 H05 direcc 40 00 172 |
# |  7 H06 comuna 25 00 212 | 8 H07 ciudad 25 00 237 | 9 H08 correo 40 00 262 |
# | 10 H09 telefo 50 00 302 |11 H10 especi 02 51 352 |12 H11 regist 06 01 354 |
# | 13 H12 fechai 10 00 360 |14 H13 fechae 10 00 370 |15 H14 zonas  03 53 380 |
# | 16 H15 status 02 52 383 |17 H16 conara 06 00 385 |

pacie=./tmp/pacie.e
./appl/sh/pacie | awk -F^ ' substr($21,1,2)=="00" && x[$1]!=1{ print $1"^"trail($2)"^"trail($5)"^"trail($3)"^"trail($4)"^"trail($19)"^"trail(sep($7))"^"trail($15);x[$1]=1 }
function sep(s) { split(s,a,"|"); return a[2]}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) } ' | sort >$pacie



# |  1 H00 indice 05 15   0 | 2 H01 codigo 13 07   5 | 3 H02 patern 40 00  18 |
# |  4 H03 matern 40 00  58 | 5 H04 nombre 40 00  98 | 6 H05 direcc 50 00 138 |
# |  7 H06 comuna 05 55 188 | 8 H07 ciudad 20 00 193 | 9 H08 sexoss 09 50 213 |
# | 10 H09 nacimt 10 00 222 |11 H10 contac 40 00 232 |12 H11 telefo 40 00 272 |
# | 13 H12 tcelul 40 00 312 |14 H13 correo 50 00 352 |15 H14 actges 10 00 402 |
# | 16 H15 renova 10 00 412 |17 H16 diagno 80 00 422 |18 H17 observ 999 14 502 |
# | 19 H18 isapre 02 51 1501 |20 H19 fechat 10 00 1503 |21 H20 status 02 53 1513 |
# | 22 H21 fecref 10 00 1515 |23 H22 client 02 56 1525 |

# 24 D00 presta  2   54 | 25 D01 modali  2   52 | 26 D02 topes   2   01 | 
# 27 D03 di      2   00 | 28 D04 di      2   00 | 29 D05 di      2   00 | 
# 30 D06 di      2   00 | 31 D07 di      2   00 | 32 D08 di      2   00 | 
# 33 D09 di      2   00 | 34 D10 di      2   00 | 35 D11 di      2   00 | 
# 36 D12 di      2   00 | 37 D13 di      2   00 | 38 D14 di      2   00 | 
# 39 D15 di      2   00 | 40 D16 di      2   00 | 41 D17 di      2   00 | 
# 42 D18 di      2   00 | 43 D19 di      2   00 | 44 D20 di      2   00 | 
# 45 D21 di      2   00 | 46 D22 di      2   00 | 47 D23 di      2   00 | 
# 48 D24 di      2   00 | 49 D25 di      2   00 | 50 D26 di      2   00 | 
# 51 D27 di      2   00 | 52 D28 di      2   00 | 53 D29 di      2   00 | 
# 54 D30 di      2   00 | 55 D31 di      2   00 | 56 D32 di      2   00 | 
# 57 D33 di      2   00 | 58 D34 di      2   00 | 59 D35 di      2   00 | 
# 60 D36 di      2   00 | 61 D37 di      2   00 | 62 D38 di      2   00 | 
# 63 D39 di      2   00 | 64 D40 total   1   01 | 


agend=./tmp/agend.e
./appl/sh/agend | awk -F^ ' {  x[substr($2,1,5)"^"substr($4,1,2)]=sep($5) }
function sep(s) { split(s,a,"|"); return a[1]}
END {
for (i in x) print  i"^"x[i]
} ' - | sort > $agend

# |  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 |
# | 10 H09 ecog00 02 01  88 |11 H10 eva000 02 01  90 |12 H11 filler 296 15  92 |


awk -F^ ' BEGIN { vacio="POR DEFINIR^^^^^"}
FILENAME == ARGV[1] {pac[$1]=$0}
FILENAME == ARGV[2] {prf[$1]=$0}
FILENAME == ARGV[3] {eqp[$1$2]=$3}
END {
for (i in pac)
{
	printf "%s^",pac[i]
        for(j=0;j<6;j++)
	{
		printf "%s%.2d\n",i,j
		printf "%s\n",sprintf("%.2d",i,j)
		printf "%s\n",eqp[sprintf("%s%.2d",i,j)]
        for(j=0;j<6;j++) if(prf[eqp[sprintf("%s%.2d",i,j)]]="") printf "%s",vacio
	else printf "%s",prf[eqp[sprintf("%.2d",i,j)]
	print ""
}
}

' $pacie $perso $agend

exit
==> tmp/agend.e <==
     ^  ^                                
     ^00^                                
     ^01^                                
     ^02^17.885.100-4
     ^03^                                
00001^00^16.203.285-2
00001^01^16.621.429-7
00001^03^17.142.186-1
00002^00^16.203.285-2
00002^01^16.621.429-7

==> tmp/pacie.e <==
00375^09.877.784-9^MARCELO^ESPINOZA^CARCAMO^07|NUEVA MAS VIDA^Machal&iacute;^06/10/2020
00387^08.938.027-8^MARIA^SIMPSON^FERRADA^07|NUEVA MAS VIDA^Concepcin^09/10/2020
00400^12.425.498-1^HUGO^HERRERA^CISTERNA^07|NUEVA MAS VIDA^Coquimbo^10/11/2020
00402^15.210.669-6^ANA^GONZALEZ^ESCALONA^07|NUEVA MAS VIDA^Laja^18/11/2020
00410^07.945.492-3^PEDRO ERNESTO^ANDRADE^VANDEN BOSCH^01|CONSALUD^Nogales^23/12/2020
00420^11.309.649-7^PEDRO^ABURTO^MUOZ^01|CONSALUD^Ancud^28/01/2021
00421^08.050.701-1^MONICA^ESPEJO^GREVE^07|NUEVA MAS VIDA^Concn^26/01/2021
00428^07.140.515-K^MARTA^ZERCOVICH^GUINEO^07|NUEVA MAS VIDA^Punta Arenas^04/02/2021
00431^16.713.616-8^MAURICIO^CASTRO^HUIDOBRO^01|CONSALUD^San Bernardo^28/01/2021
00436^09.263.639-9^ERNESTO^RETAMALES^NEGRETE^07|NUEVA MAS VIDA^Antofagasta^18/02/2021

==> tmp/perso.e <==
07.477.723-6^Norma Saez^07.477.723-6^30783^9 79908864^saezbayer@gmail.com
07.837.871-9^Mario Pea y Lillo^07.837.871-9^32253^9 75484206^linares@mutual.cl
07.984.478-0^Marjorie Arias^07.984.478-0^423640^9 83704561^mayoarias@gmail.com
08.945.375-5^Jacqueline Morn^08.945.375-5^177327^9 61161888^morandptosalud@gmail.com
09.085.723-1^Alexandra Ogaz^09.085.723-1^107603^9 91883214^alexaogaz@yahoo.com
09.433.815-8^Marcelo Figueroa^09.433.815-8^327114^9 96389412^marcelofiguero@gmail.com
10.047.486-7^Omar Ahumada^10.047.486-7^98530^9 65177839^omar.ahumadag@gmail.com
10.090.685-6^Marcos Bugueo^10.090.685-6^225492^9 98418969^mbc.psicologo@gmail.com
10.098.858-5^Aura Ziga^10.098.858-5^62771^9 93278640^nyzu52@gmail.com
10.107.125-1^Barbara Huenchuguala^10.107.125-1^224703^9 66935808^barhlpa@gmail.com
