Say I have these two input files: > file1234> file2101001000And I would like to compute: file1/file2 for each line, resulting in file3:> file 30.20.030.004Divisions in bash can be achieved by: $((file1_line/file2_line))
arithmetic operations between files
text processing;arithmetic
A paste and bc combination is a good choice for simple arithmetic:paste -d/ file1 file2 | bc -lOutput:.2000000000.0300000000.0040000000A more advanced exampleWith some trickery you can get a bit more complicated. Say file3 contains:678You can do (file1 + file3) / file2 like this:paste -d'(+)/' /dev/null file1 file3 /dev/null file2Output:(2+6)/10(3+7)/100(4+8)/1000This works because paste cycles through its delimiter list for each line.React to divide-by-zeroIlligal operations sent to bc result in a warning being sent to standard error. You could redirect these to a different file and decide program flow based on its content, e.g.:paste -d/ file1 file2 | bc -l > resultfile 2> errorfileif grep -q 'Divide by zero' errorfile; then  echo Error in calculationelse  echo All is wellfiOr if there was any error:paste -d/ file1 file2 | bc -l > resultfile 2> errorfileif ! file errorfile | grep -q empty; then  echo Error in calculationelse  echo All is wellfi