Tengo 2 archivos grandes en el sistema Unix que tienen miles de filas y alrededor de 80 columnas cada uno. He ordenado los archivos según el grupo de claves únicas para que siempre comparemos las mismas filas. Para facilitar la comprensión, estoy dando solo 3 filas y 7 columnas aquí.
Archivo 1:
d_report_ref_date="2021-03-31" system_id="VTX" contract_id="1130" credit_line_cd="ABC123" contract_id="ABC123" src_system_id="PRA" entity_cd="U0525" d_report_ref_date="2021-03-31" system_id="VTX" contract_id="1130" credit_line_cd="ABC124" contract_id="ABC124" src_system_id="PRA" entity_cd="U0526" d_report_ref_date="2021-03-31" system_id="VTX" contract_id="1130" credit_line_cd="ABC125" contract_id="ABC125" src_system_id="PRA" entity_cd="U0527"
Archivo2:
d_report_ref_date="2021-03-31" system_id="VTX" contract_id="1130" credit_line_cd="ABC123" contract_id="ABC123" src_system_id="PRA" entity_cd="U0525" d_report_ref_date="2021-03-31" system_id="VTX" contract_id="1130" credit_line_cd="ABC124" contract_id="ABC124" src_system_id="PRB" entity_cd="V0528" d_report_ref_date="2021-03-31" system_id="VTX" contract_id="1130" credit_line_cd="ABC125" contract_id="ABC125" src_system_id="PRA" entity_cd="U0530"
Rendimiento esperado:
Mismatch in row 2 : file1.src_system_id=PRA file2.src_system_id=PRB, file1.entity_cd=U0526 file2.entity_cd=V0528 Mismatch in row 3 : file1.entity_cd=U0527 file2.entity_cd=U0530
¿Es posible lograr esto usando bash scripting? Probé AWK que no me está dando el resultado deseado.
paste -d' ' file1 file2| awk -F' ' '{w=NF/2; for(i=1;i<=w;i++) if($i!=$(i+w)) printf "%d %d %s %s", NR,i,$i,$(i+w); print ""}'
Gracias de antemano !!!
Usando cualquier awk en cualquier shell en cada caja de Unix:
$ cat tst.awk BEGIN { FS="[= ]" } NR==FNR { for (i=1; i<NF; i+=2) { file1[NR,i] = $(i+1) } next } { msg = sep = "" for (i=1; i<NF; i+=2) { if ( $(i+1) != file1[FNR,i] ) { msg = msg sep " " ARGV[1] "." $i "=" file1[FNR,i] " " FILENAME "." $i "=" $(i+1) sep = "," } } if ( msg != "" ) { print "Mismatch in row " FNR " :" msg ORS } }
$ awk -f tst.awk file1 file2 Mismatch in row 2 : file1.src_system_id="PRA" file2.src_system_id="PRB", file1.entity_cd="U0526" file2.entity_cd="V0528" Mismatch in row 3 : file1.entity_cd="U0527" file2.entity_cd="U0530"
Lo anterior supone:
=
o espacios en blancoEche un vistazo a wdiff
, algo como esto podría funcionar:
$ wdiff -w$'\e[31m' -x $'\e[0m' -y $'\e[32m' -z $'\e[0m' file1 file2
Las opciones -wxyz
son para definir prefijos y sufijos para eliminación e inserciones respectivamente. En este caso, hacemos un intento ingenuo de colorear las eliminaciones en rojo y las inserciones en verde.