Say I have these two input files:
> file1
2
3
4
> file2
10
100
1000
And I would like to compute: file1/file2 for each line, resulting in file3:
> file 3
0.2
0.03
0.004
Divisions in bash can be achieved by: $((file1_line/file2_line))
arithmetictext processing
Say I have these two input files:
> file1
2
3
4
> file2
10
100
1000
And I would like to compute: file1/file2 for each line, resulting in file3:
> file 3
0.2
0.03
0.004
Divisions in bash can be achieved by: $((file1_line/file2_line))
Best Answer
A
paste
andbc
combination is a good choice for simple arithmetic:Output:
A more advanced example
With some trickery you can get a bit more complicated. Say
file3
contains:You can do
(file1 + file3) / file2
like this:Output:
This works because
paste
cycles through its delimiter list for each line.React to divide-by-zero
Illigal 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.:Or if there was any error: