PyariBilli
PyariBilli

Reputation: 531

average lines from two files and put into one file

I have two files, foo.dat and bar.dat, which can be thought of as m by n matrices. I want to produce a file, cum.dat, representing the matrix cum[i,j] = (foo[i,j]+bar[i,j]) / 2.

For example, if the contents of the files are as follows:

foo.dat :
  11 21
  12 22
  13 23
  14 24
  15 25
  16 26

bar.dat :
  12 22
  13 23
  14 24
  15 25
  16 26
  17 27

I want cum.dat to be as follows:

  11.5 21.5
  12.5 22.5
  13.5 23.5
  14.5 24.5
  15.5 25.5
  16.5 26.5

Upvotes: 0

Views: 46

Answers (1)

anubhava
anubhava

Reputation: 785156

You can use awk:

awk 'NR==FNR{c1[FNR]=$1; c2[FNR]=$2; next} {
     printf "%.1f %.1f\n", ($1+c1[FNR])/2, ($2+c2[FNR])/2}' foo.dat bar.dat

11.5 21.5
12.5 22.5
13.5 23.5
14.5 24.5
15.5 25.5
16.5 26.5

Upvotes: 1

Related Questions