awk script concatenate two column and perform mutiplication


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script concatenate two column and perform mutiplication
# 1  
Old 04-03-2018
awk script concatenate two column and perform mutiplication

Need your help in solving this puzzle. Any kind of help will be appreciated and link for any documents to read and learn and to deal with such scenarios would be helpful

Concatenate column1 and column2 of file 1. Then check for the concatenated value in Column1 of File2. If found extract the corresponding value of column2 and column3 of File2, Again concatenate column1 and column2 of File2. Now look for this concatenated value in File1 and if found

For example - concatenate column1(262881626) and column2(10) of File1. Then look for this concatenated(26288162610) value in column1 of File2 and extract corresponding column2 and column3 value of File2. Now again concatenate column1 and column2 of File2 and look for this concatenated(2628816261050) value in File1 and multiply exchange rate(2) fetched by concatenated value(26288162610) with taxable value(65) which corresponding to 2628816261050 of File1. Store the result of multiplcation value in column4(AD) of File1 only

I tried below code..but its not working.

awk -F"|" 'FNR==NR{a[$1$2]=$NF;next} {print $0,$1 in a?"|" a[$1]*$NF:""}' OFS="" File2 File1

File1

Code:
 Bill Doc     LineNo     Taxablevalue     AD
 262881626         10        245
 262881627         10        32
 262881628         20        456
 262881629         30         0
 262881630         40         45
2628816261050      11        65
2628816271060      12        34
2628816282070      13        45
2628816293080      14        0
2628816304090      15

File2

Code:
Bill.Doc     Item     Exch.Rate     
26288162610     50     2    
26288162710     60     1    
26288162820     70     45
26288162930     80     1    
26288163040     90     5

Output File

Code:
Bill Doc        LineNo  Taxablevalue   AD
262881626        10       245
262881627        10       32
262881628        20       456
262881629        30       0
262881630        40            
2628816261050     11      65            130
2628816271060     12      34            34
2628816282070     13      45            180
2628816293080     14      0              0
2628816304090     15

# 2  
Old 04-03-2018
In what way "its not working"? Usually, contemplating / scrutinizing misbehaviour and error messages will lead you to the root cause yourself, and the knowledge of either would help others help you as well.
# 3  
Old 04-03-2018
Hi Rudic,
it is not giving any error, but also not producing the desired output and not doing multiplication of column3 of File1 with column3 of file2 and not storing the multiplication result in neither column3 nor column4 of File1..
Pleas help rudic to achieve my output
# 4  
Old 04-03-2018
Well, you define the field separator to be | , but I don't see ANY | in your sample data. Did you consider dropping the -F"|"?

Try as well
Code:
awk 'NR==FNR {EXR[$1 $2] = $3; next} $1 in EXR {$4 = $3 * EXR[$1]} 1' OFS="\t" file2 file1

The third exchange rate seems a bit high and thus leads to a rather high AD value...

Last edited by RudiC; 04-03-2018 at 06:26 AM..
# 5  
Old 04-03-2018
Hi Rudic,
Apologies, but actually to have more clarity for sample data i pasted sample file1 and file2 without pipe..though in actual file contains pipe..
for now code point of view you can cnsider space as well
# 6  
Old 04-03-2018
Hmmm - it doesn't really help if you post code and sample data that don't match.
Howsoever, did you solve your problem or does it persist?
# 7  
Old 04-03-2018
Hi Rudic,

Still have problem. getting 0 in column for all rows in which i am storing multiplication result.

---------- Post updated at 05:00 AM ---------- Previous update was at 04:57 AM ----------

Hi Rudic,
can you please explain the code as what variable is pointing towards which file

Code:
awk 'NR==FNR {EXR[$1 $2] = $3; next} $1 in EXR {$4 = $3 * EXR[$1]} 1' OFS="\t" file2 file1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. Shell Programming and Scripting

awk script to append suffix to column when column has duplicated values

Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2 Scenario 1 i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

Group/concatenate certain column and basis on this do addition on other column

Hi Experts, Need your support I want to group/concatenate column 1,2,12 and 13 and if found duplicate then need to sum value of column 17,20,21 and column22. After concatenation if found unique then no action to be taken. Secondly want to make duplicate rows basis on grouping/concatenation of... (1 Reply)
Discussion started by: as7951
1 Replies

4. Shell Programming and Scripting

awk script to find data in three file and perform replace operation

Have three files. Any other approach with regards to file concatenation or splitting, etc is appreciated If column55(billngtype) of file1 contains YMNC or YPBC then pick the value of column13(documentnumber). Now find this documentnumber in column1(Billdoc) of file2 and grep the corresponding... (4 Replies)
Discussion started by: as7951
4 Replies

5. Shell Programming and Scripting

Concatenate values in the first column based on the second column.

I have a file (myfile.txt) with contents like this: 1.txt apple is 3.txt apple is 5.txt apple is 2.txt apple is a 7.txt apple is a 8.txt apple is a fruit 4.txt orange not a fruit 6.txt zero isThe above file is already sorted using this command: sort -k2 myfile.txtMy objective is to get... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

6. Shell Programming and Scripting

Filter on one column and then perform conditional calculations on another column with a Linux script

Hi, I have a file (stats.txt) with columns like in the example below. Destination IP address, timestamp, TCP packet sequence number and packet length. destIP time seqNo packetLength 1.2.3.4 0.01 123 500 1.2.3.5 0.03 44 1500 1.3.2.5 0.08 44 1500 1.2.3.4 0.44... (12 Replies)
Discussion started by: Zooma
12 Replies

7. Shell Programming and Scripting

awk script to perform an action similar to vlookup between two csv files in UNIX

Hi, I am new to awk/unix and am trying to put together an awk script to perform an action similar to vlookup between the two csv files. Here are the contents of the two files: File 1: Date,ParentID,Number,Area,Volume,Dimensions 2014-01-01,ABC,247,83430.33,857.84,8110.76... (9 Replies)
Discussion started by: Prit Siv
9 Replies

8. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

9. Shell Programming and Scripting

awk script to (un)/concatenate fields in file

Hi everyone, I'm trying to use the "join" function for more than 1 field. Since it's not possible as it is, I want to take my input files and concatenate the joining fields as 1 field (separated by "|"). I wrote 2 awk script to do and undo it (see below). However I'm new to awk and I'm certain I... (5 Replies)
Discussion started by: anthony.cros
5 Replies

10. Shell Programming and Scripting

Sed or awk script to remove text / or perform calculations from large CSV files

I have a large CSV files (e.g. 2 million records) and am hoping to do one of two things. I have been trying to use awk and sed but am a newbie and can't figure out how to get it to work. Any help you could offer would be greatly appreciated - I'm stuck trying to remove the colon and wildcards in... (6 Replies)
Discussion started by: metronomadic
6 Replies
Login or Register to Ask a Question