0

I have 2 files.

File 1 contains :-

emcas_biaas_dev=-628
emcas_brs_ba=-32
emcas_cc_analytics=246
emcas_edservices_bi=312
emcas_edservices_dev=-159
emcas_engr_usd=1519
emcas_engr_xio=-67
emcas_fin_bi=9

and File 2 contains :-

emcas_bdl_migrate=2
emcas_biaas_dev=63
emcas_brs_ba=430
emcas_cc_analytics=2231
emcas_clm_reporting=16
emcas_collab_xsat=6241
emcas_cpsd_cee=11
emcas_cpsd_hcp=0
emcas_e2ep_ba=187
emcas_edservices_bi=955
emcas_edservices_dev=374
emcas_elms_analytics=0
emcas_engr_clm=3
emcas_engr_scaleio=0
emcas_engr_usd=2669
emcas_engr_xio=340
emcas_fin_bi=333
emcas_fin_drr=5528

I want the desired output in a separate file as :-

emcas_biaas_dev=-628=63
emcas_brs_ba=-32=430
emcas_cc_analytics=246=2231
emcas_edservices_bi=312=955
emcas_edservices_dev=-159=374
emcas_engr_usd=1519=2669
emcas_engr_xio=-67=340
emcas_fin_bi=9=333

2 Answers 2

2

I take it you want to output only fields whose key appears in both files, with both values. As mentioned in Sparhawk’s answer, the right tool for this is join. With AWK, the following works:

#!/usr/bin/awk -f

BEGIN {
    OFS = FS = "="
}

FNR == NR {
    values[$1] = $2
}

FNR < NR {
    if (values[$1]) {
        print $1, values[$1], $2
    }
}

This starts by setting the input and output field separators to “=”, then parses the first file (FNR == NR), storing all its values in the values associative array, then it parses any following file (FNR < NR), and for each line, if a value was seen in the first file, outputs the key, the stored value, and the value in the current file.

1

For this task, a simpler tool than awk is join:

join -t '=' -1 1 -2 1 file1.txt file2.txt

Explanation

See man join for more information, but:

  • -t '=': use = as delimiter
  • -1 1 -2 1: join on field 1 of file 1, and field 1 of file 2
  • file1.txt file2.txt: join these two files.
1
  • It works well..!! Commented Aug 27, 2018 at 10:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .