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.

  • Your unaffected output columns appear to come from file2. Is that correct? In your output you have changed columns 8 and 12. What is the rule for those? – roaima Feb 3 '15 at 21:25
  • the system does not allows me to edit as I want. The format is the one I posted as an answer by mistake. – euge1780 Feb 3 '15 at 21:42
  • ok. I edited now... – euge1780 Feb 3 '15 at 21:45
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.

  • I edited the post. I think now is better understood. Sorry, but the system edited first in a wrong way. – euge1780 Feb 3 '15 at 21:47
  • 2
    paste file1 suma_file2 | awk '{ print $4 " " $5 " " $6 " " $3 + $7; }' worked for me – Emilio Galarraga Apr 19 '17 at 22:17
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

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.