-1

How to add the two columns in different two files.

File1 1 45 2 56 3 56

File2

1 2 2 3 3 4

The Output should come like below 1 47 2 59 3 60

Thanks

2 Answers2

1

You could write a simple script in php.

<?php 

$f1 = file_get_contents('/path/to/file1');
$f2 = file_get_contents('/path/to/file2');

$cols1 = explode(' ',$f1);
$cols2 = explode(' ',$f2);

for($i=0; $i<sizeof($cols1); $i++) {
   echo $cols[$i] + $cols2[$i] . ' ';
}

?>

And if you're going to use this function many times, you could add the ability to use CLI parameters to the script instead of hard coding the file paths.

iamdev
  • 668
  • 3
  • 13
  • Can you share us that how to add the two values in linuxcommand – user2175455 May 19 '14 at 20:50
  • Do you mean shell programming or a standard linux program? I'm sure you could do it with bash, for example, but I'm not familiar enough with bash. I wouldn't be surprised if there is a pre-installed linux parsing tool to solve this problem, but I don't know of one. – iamdev May 19 '14 at 20:53
0

Try awk and sed and by piping them (i.e. |) you can write a onliner to do that for you. Or you can use Perl -e and write a one-liner.

Zuzlx
  • 1,140
  • 10
  • 28
  • see this http://stackoverflow.com/questions/1632113/what-is-the-difference-between-sed-and-awk – Zuzlx May 19 '14 at 20:58