if [ "$1" == "" ]
then
echo Periodo 01-12 para 2022
exit
fi

PER=2022-$1
BIT=$PER/BIT
rm -f $BIT
for i in $PER/B[0123456789]*.txt
do
awk ' { print substr($0,1,length-1) } ' $i | 
awk -F"	" ' BEGIN { t=0 }
/Buscar/ { t=1; next }
t==0 {next }
/Mostrando/ { exit }
$1+0 >0 {t=2 }
t==1 {
##for(i=1;i<=NF;i++) printf "%s^",$i
z+=NF
#if(z==139) print ""
next
}
t==2 { if(substr($1,1,7)+0>9000+0) {r=0 ;print ""
#printf "\n%s:%5d-!!!!-%10.10s  $1=%s ES MAYOR A 10000-!!!!--",FILENAME,NR,$1,substr($1,1,7)
}
rr +=NF
#print  $0,NF
if(NF==1) rr=1
#if (NF==1 && rr=1) printf "TXT:%s ",$0
#printf "\nNF=%d - %s-- r=%d///\n",NF,$0,r
if(rr!=1)
if(NF==2 && r==13)  printf " %s",$1
if(substr($0,1,1)=="	")
for(i=1;i<NF;i++)
{
	if(i==1) printf "%s",$i
	else for(i=2;i<=NF;i++) printf "^%s",$i
}
else for(i=1;i<=NF;i++) printf "^%s",$i
rr=0
}
END {
print ""
}



 '
done >$BIT
cd ..
pacie=./tmp/pacie.f
./appl/sh/pacie >$pacie

perso=./tmp/perso.f
./appl/sh/perso >$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 |


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

cd TMC
awk -F^ '
function trail(txt) { while (substr(txt,length(txt),1)==" ") txt=substr(txt,1,length(txt)-1); return (txt) }

FILENAME == ARGV[1] { x=trail($2)
if(substr(x,1,1)=="0") x=substr(x,2)
pac[x]=1;next }
FILENAME == ARGV[2] { x=trail($1)
if(substr(x,1,1)=="0") x=substr(x,2)
prf[x]=1;next }
#{ print $11,pac[trail($11)]}
#{ print $9,prf[trail($9)]}
#NF > 3 { print $(NF-3)}
NF >3 && index($(NF-3),"'"$PER"'") && pac[toupper(trail($11))]+0 && prf[toupper(trail($9))]+0  { for (i=2;i<13;i++) printf "%s^",toupper($i)
print $(NF-3)"^"$(NF-2)"^"$(NF-1)"^"$NF } ' .$pacie .$perso $PER/BIT | awk -F^ ' {
 z=$10$8$(NF-3)
 y[z]++
#if( y[z]>1 ) print $0"\n"x[z]
 x[z]=$0
}
END { 
for (i in x) print x[i]
 } ' >$PER/BIT$1
