how to multiply a range of elements in a line by a constat

81 views Asked by At

I have a file with several columns and I want to multiply by a constant the first half elements of these columns over a range of lines. For example, if I had only 4 columns and 2 lines:

 8  2 4 5
 6 12 8 8 

The output I want (supposing the multiplicative constant is 1/2) would be the following:

4 1 4 5
3 6 8 8

I have no glue how to do it, I suppose that bc will help...

1

There are 1 answers

0
anubhava On BEST ANSWER

You can use awk like this for processing your file on row/col basis:

awk -v n=2 '{for (i=1; i<=NF/2; i++) $i/=n} 1' file
4 1 4 5
3 6 8 8