Help With awk/sed

Hi All,

I have two files with the following content

File 1

Sep,Sep,Sep,Sep,Sep
AC1,AC2,AC3,AC4,AC5
G1,G1,G1,G1,G1
T1,13,AED, 0 , 0 , 0 , 0 , 0 
T1,13,AUD, 6943952.22 , 0 , 0 , 3798.34 , 0 
T1,13,BDT, 0 , 0 , 0 , 0 , 0 
T1,13,BGN, 0 , 0 , 0 , 0 , 0 
T1,13,BRL, 0 , 0 , 0 , 0 , 0 
T1,13,CAD, 9649,856.90 , 2467977.74 , 450046.50 , 36819,975.15 , 13576999.88 
T1,13,CHF, 2173182.34 , 0 , 0 , 0 , 0 
T1,13,CNY, 0 , 0 , 0 , 0 , 0 
T1,13,CZK, 17738.54 , 0 , 0 , 0 , 0 
T1,13,DKK, 1620681.04 , 0 , 0 , 0 , 0 

The amounts are present at intersections like

T1 13 AED(horizontal),Sep AC1 G1(Vertical) is having a value of '0'and likewise

File 2

AED,0.2722663
ARS,0.1893393
AUD,0.9693415
BDT,0.0128065
BGN,0.6600884
BND,0.7906279
BRL,0.4855461
CAD,0.9665699
CHF,1.0321366
CLP,0.002042

My requirement is that i need to get the amounts in a 3rd file like, search for 'AUD' in first file, and multiply all the values there with the value in second file against 'AUD'

eg :
File 3

Sep,Sep,Sep,Sep,Sep
AC1,AC2,AC3,AC4,AC5
G1,G1,G1,G1,G1
T1,13,AED, 0 , 0 , 0 , 0 , 0 
T1,13,AUD, 6731061.06 , 0 , 0 , 3681.89 , 0 

you may try

$ cat file1
AED,0.2722663
ARS,0.1893393
AUD,0.9693415
BDT,0.0128065
BGN,0.6600884
BND,0.7906279
BRL,0.4855461
CAD,0.9665699
CHF,1.0321366
CLP,0.002042
$ cat file2
Sep,Sep,Sep,Sep,Sep
AC1,AC2,AC3,AC4,AC5
G1,G1,G1,G1,G1
T1,13,AED, 0 , 0 , 0 , 0 , 0 
T1,13,AUD, 6943952.22 , 0 , 0 , 3798.34 , 0 
T1,13,BDT, 0 , 0 , 0 , 0 , 0 
T1,13,BGN, 0 , 0 , 0 , 0 , 0 
T1,13,BRL, 0 , 0 , 0 , 0 , 0 
T1,13,CAD, 9649,856.90 , 2467977.74 , 450046.50 , 36819,975.15 , 13576999.88 
T1,13,CHF, 2173182.34 , 0 , 0 , 0 , 0 
T1,13,CNY, 0 , 0 , 0 , 0 , 0 
T1,13,CZK, 17738.54 , 0 , 0 , 0 , 0 
T1,13,DKK, 1620681.04 , 0 , 0 , 0 , 0
$ awk  -F, 'FNR==NR{_[$1]=$2;next}{x=($3 in _)?_[$3]:1}{for(i=1;i<=NF;i++)printf (i<NF)?(i!=2 && $i!~/[A-Za-z]/ && $i!=0 )? sprintf("%5.2f",$i*x) OFS: $i OFS:(i!=2 && $i!~/[A-Za-z]/ && $i!=0)? sprintf("%5.2f",$i*x): $i;printf RS}' OFS=\,  file1 file2

Resulting

Sep,Sep,Sep,Sep,Sep
AC1,AC2,AC3,AC4,AC5
G1,G1,G1,G1,G1
T1,13,AED, 0 , 0 , 0 , 0 , 0 
T1,13,AUD,6731061.06, 0 , 0 ,3681.89, 0 
T1,13,BDT, 0 , 0 , 0 , 0 , 0 
T1,13,BGN, 0 , 0 , 0 , 0 , 0 
T1,13,BRL, 0 , 0 , 0 , 0 , 0 
T1,13,CAD,9326.43,828.25,2385473.00,435001.40,35588.14,942.55,13123119.42
T1,13,CHF,2243021.03, 0 , 0 , 0 , 0 
T1,13,CNY, 0 , 0 , 0 , 0 , 0 
T1,13,CZK,17738.54, 0 , 0 , 0 , 0 
T1,13,DKK,1620681.04, 0 , 0 , 0 , 0

Thanks for the reply Akshay, but the second column '13' in the original code has to remain as it is, without any changes.

Thanks again in advance :slight_smile:

@dev.devil.1983

try .. I modified it

1 Like

How about

awk -F, 'NR==FNR        {C[$1]=$2;next}
         ($3 in C)      {for (i=4; i<=NF; i++) $i*=C[$3]}
         1
        ' CONVFMT="%.2f" OFS="," file2 file1

?

1 Like

Nice Sir !