0

I have two files and I want to merge them in a way when some specific columns are the same, export those lines with all fields except one column, which to be the subtraction of the two files in that specific column.

For example, I have two files as follows and I want to output the lines where columns 1 to 3 are the same, and with column 4 equal to the subtraction of the two values in the files' column 4(File1 - File2).

File 1:

10      20      30      50     
100     200     300     850    
150     250     350     900    
.....

FIle2:

100     200     300      840    
15      25      35       55     
150     250     350      880    
....

output:

100     200     300      10    
150     250     350      20
....    
1

Using Awk:

$ awk '{k = $1 FS $2 FS $3} NR==FNR {a[k] = $4; next} k in a {print k, a[k]-$4}' File1 File2
100 200 300 10
150 250 350 20
  • Thanks for your response. It works well. – Esi Jul 26 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.