'Comparison of two file in Unix and display differences
I have 2 files on Unix. I want to compare the files-
- To show the the rows which are missing in both files.
- To show the actual differences in both files.
- To be able to skip the columns for comparison which I don't want in report for e.g. d_report_ref_date
- My unique key for comparison is v_party_id
File 1:
d_report_ref_date="2021-03-31" n_pd_percent="0.16323687" v_accounting_standard="IFRS" v_party_default_status_cd="NOTDFLT" v_party_id="103811925" v_src_system_id="SMT"
d_party_default_status_date="2012-03-09" d_report_ref_date="2021-03-31" n_pd_percent="1" v_accounting_standard="SQRT" v_party_default_status_cd="UNLIKE" v_party_id="36056030" v_src_system_id="SMT"
d_report_ref_date="2021-03-31" n_pd_percent="0.16323687" v_accounting_standard="IFRS" v_party_default_status_cd="NOTDFLT" v_party_id="53565979" v_src_system_id="SMT"
d_report_ref_date="2021-03-31" n_pd_percent="0.16323687" v_accounting_standard="IFRS" v_party_default_status_cd="NOTDFLT" v_party_id="103811100" v_src_system_id="SMT"
d_report_ref_date="2021-03-31" n_pd_percent="0.16323687" v_accounting_standard="IFRS" v_party_default_status_cd="NOTDFLT" v_party_id="103811200" v_src_system_id="SMT"
File 2
d_report_ref_date="2021-03-31" n_pd_percent="0.2045" v_accounting_standard="SQRT" v_party_default_status_cd="NOTDFLT" v_party_id="103811925" v_src_system_id="SMT"
d_party_default_status_date="2012-03-09" d_report_ref_date="2021-03-31" n_pd_percent="1" v_accounting_standard="IFRS" v_party_default_status_cd="UNLIKE" v_party_id="36056030" v_src_system_id="SMT"
d_report_ref_date="2021-03-31" v_accounting_standard="IFRS" v_party_default_status_cd="NOTDFLT" v_party_id="53565979" v_src_system_id="SMT"
d_report_ref_date="2021-03-31" n_pd_percent="0.16323687" v_accounting_standard="IFRS" v_party_default_status_cd="NOTDFLT" v_party_id="103811400" v_src_system_id="SMT"
File3
period="2021-02-28" book_base_ent_cd="U0027" other_inst_ident="PLCHS252SA20" rep_nom_curr="PLN" reporting_basis="Unit" src_instr_class="Debt" mat_date="2028-02-25" nom_curr="PLN" primary_asset_class="Bond" seniority_type="931" security_status="alive" issuer_name="CUST38677608" intra_group_prud_scope="Issuer is not part of the reporting group" intra_group_acc_scope="Issuer is not part of the reporting group" frbrnc_stts="Not forborne or renegotiated" src_frbrnc_stts="NOFRBRNRNGT" prfrmng_stts="Performing" src_prfrmng_stts="KC10.1" dflt_stts_issr="Not in default" src_dflt_stts_issr="KC10.1" dflt_stts_instrmnt="Not in default" src_mes_accntng_clssfctn="AMC" prdntl_prtfl="Non-trading book" imprmnt_stts="Stage 1 (IFRS)" src_imprmnt_stts="1" imprmnt_assssmnt_mthd="Collectively assessed" src_imprmnt_assssmnt_mthd="COLLECTIVE" accmltd_imprmnt="392.69" accmltd_chngs_fv_cr="0" expsr_vl="0" unit_measure="EUR" unit_measure_nv="EUR" crryng_amnt="122825.65" issuer_grid_id="38677608" v_party_id="PLCHS252SA20"
File4
period="2021-02-28" book_base_ent_cd="U0027" other_inst_ident="PLCHS252SA20" rep_nom_curr="PLN" reporting_basis="Unit" src_instr_class="Debt" mat_date="2028-02-25" nom_curr="PLN" primary_asset_class="Bond" seniority_type="931" security_status="alive" issuer_name="CUST38677608" intra_group_prud_scope="Issuer is not part of the reporting group" intra_group_acc_scope="Issuer is not part of the reporting group" frbrnc_stts="Not forborne or renegotiated" src_frbrnc_stts="NOFRBRNRNGT" prfrmng_stts="Performing" src_prfrmng_stts="KC10.1" dflt_stts_issr="Not in default" src_dflt_stts_issr="KC10.1" dflt_stts_instrmnt="Not in default" src_mes_accntng_clssfctn="AMC" prdntl_prtfl="Non-trading book" imprmnt_stts="Stage 1 (IFRS)" src_imprmnt_stts="1" imprmnt_assssmnt_mthd="Collectively assessed" src_imprmnt_assssmnt_mthd="COLLECTIVE" accmltd_imprmnt="392.69" accmltd_chngs_fv_cr="0" expsr_vl="0" unit_measure="EUR" unit_measure_nv="EUR" crryng_amnt="122825.65" issuer_grid_id="38677608" v_party_id="PLCHS252SA20"
Expected Output
Rows missing in file1: v_party_id="103811400"
Rows missing in file2: v_party_id="103811100", v_party_id="103811200"
Mismtach in row 1 for v_party_id="103811925": file1.n_pd_percent="0.16323687" file2.n_pd_percent="0.2045", file1.v_accounting_standard="IFRS" file2.v_accounting_standard="SQRT"
Mismtach in row 2 for v_party_id="36056030": file1.v_accounting_standard="SQRT" file2.v_accounting_standard="IFRS"
Code:
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 OFS ARGV[1] "." $i "=" file1[FNR,i] OFS FILENAME "." $i "=" $(i+1)
sep = ","
}
}
if ( msg != "" ) {
print "Mismtach in row " FNR msg
}
}
Actual Output
awk -f compare.awk file1 file2
Mismtach in row 1 file1.n_pd_percent="0.16323687" file2.n_pd_percent="0.2045", file1.v_accounting_standard="IFRS" file2.v_accounting_standard="SQRT"
Mismtach in row 2 file1.v_accounting_standard="SQRT" file2.v_accounting_standard="IFRS"
Mismtach in row 3 file1.v_accounting_standard="0.16323687" file2.v_accounting_standard="IFRS", file1.v_party_default_status_cd="IFRS" file2.v_party_default_status_cd="NOTDFLT", file1.v_party_id="NOTDFLT" file2.v_party_id="53565979", file1.v_src_system_id="53565979" file2.v_src_system_id="SMT"
Mismtach in row 4 file1.v_party_id="103811100" file2.v_party_id="103811400"
What changes can I do in the code to display the output in desired format?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
