0

I have two files like this:

File1:

-54.005 -67.405 0.1

-54.05 -68.7833 0.2 

-54.05 -68.7333 0.3

File2:

-54.005 -67.4050   70    12.7

 -54.05 -68.7833  167    12.5

 -54.05 -68.7333  205    12.6

I would like to get a file that sums column 3 from file1 with column 4 from file2 with the following output:

-54.005 -67.4050   70    12.8

 -54.05 -68.7833  167    12.7

 -54.05 -68.7333  205    12.9

I tried with the following command:

cat file1 | awk '{n=$3; getline <"file2"; print "" $1,$2,$3,n+$4}' > output.txt

but it did not succeed and I couldn't find this type of answer in this site.

user40524
  • 294

2 Answers2

1

There is a tool that could merge two files into one: merging text files into one

paste file1 file2 | awk '{ print $3 + $13; }'

Add other columns to this, but note that columns in file 2 start with $10.

myaut
  • 1,431
1

You can make use of NR and FNR in awk.

awk 'FNR==NR { _a[FNR]=$3;} NR!=FNR { $4 += _a[FNR]; print;  }'  file1 file2