pc=./tmp/pc
paci=./tmp/paci
./appl/sh/pacie  | awk -F"^" ' BEGIN { 
x="15,20,16,1,2,3,4,5,7,8,19,"
r=split(x,y,",")
}
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
$1!=ant {
	for (i=1;i<r;i++) printf "%s^",trail($y[i])
	print ""
	ant=$1
}
{
	print $1"^"$22"^"$23 >"'"$pc"'"
 } ' >$paci
perso=./tmp/perso
./appl/sh/perso | awk -F"^" ' BEGIN { 
x="1,2,11,"
r=split(x,y,",")
}

function sacap(txt) { for (i=1;i<=length(txt);i++) if(substr(txt,i,1)!=".")tx=tx""substr(txt,i,1) ; return (tx) }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
{
	printf "%s^",sacap($1)
	for (i=2;i<r;i++) printf "%s^",trail($y[i])
	print ""
	tx=""
} ' -  >$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 zonas  03 53 354 |
# | 13 H12 status 02 52 357 |

agend=./tmp/agend
./appl/sh/agend | awk -F"^" ' BEGIN { 
x="1,2,3,4,5,6,7,"
r=split(x,y,",")
}
FILENAME==ARGV[1] { pac[$4]=$5 ; next} 

function sacap(txt) { tx="";for (i=1;i<=length(txt);i++) if(substr(txt,i,1)!=".")tx=tx""substr(txt,i,1) ; return (tx) }
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }
$7=="C" {
	printf "%s^",$1
	printf "%5.5s^",$2
	printf "%s^",sacap(pac[sprintf("%5.5s",$2)])
	printf "%s^",$3
	printf "%s^",$4
	printf "%s^",sacap(sprintf("%12.12s",$5))
	printf "%s^",$6
	print ""
} ' $paci - 


# |  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 |

