How do I sum lines of two files with each other? (1 column of data each) in bash
I think that paste
and bc
is the best solution here. Just for fun, here's one way to do it with pure bash (adapted from this post on unix.sx):
while read n1 <&3 && read n2 <&4; do
echo $((n1 + n2))
done 3<file1.log 4<file2.log
Or use the -u fd
c parameter read
(thanks rici):
while read -u3 n1 && read -u4 n2; do
echo $((n1 + n2))
done 3<file1.log 4<file2.log
Output:
246 912 1578
source to share
Here's a Perl solution:
perl -lne 'BEGIN{open $in1,shift; open $in2,shift} while($n1=<$in1> and $n2=<$in2>){print $n1+$n2}' file1.log file2.log
A more readable (but slightly slower) version:
open my $in1, "<", shift;
open my $in2, "<", shift;
while ( my $n1 = <$in1> and my $n2 = <$in2> ) {
print $n1 + $n2
print "\n";
}
close($in1);
close($in2);
Just for kicks, I compared the above and all the other solutions listed here. They all produce the same result.
Input files are lists of integers from 1 to 999999seq 999999 > file1.log ; seq 999999 > file2.log
Test results (10 runs each):
shell@thor 0.070/s
paste@cyrus 0.410/s
awk@anubhava 0.546/s
perl_5.6.1 1.06/s
paste@veerendra 1.32/s
perl_5.20 (readable version) 1.37/s
awk@karakfa (awk 3.1.5) 1.44/s
perl_5.20 1.75/s
source to share