Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract a constant number from a column

Tags:

Is there a way to subtract the smallest value from all the values of a column? I need to subtract the first number in the 1st column from all other numbers in the first column.

I wrote this script, but it's not giving the right result:

$ awk '{$1 = $1 - 1280449530}' file  1280449530 452 1280449531 2434 1280449531 2681 1280449531 2946 1280449531 1626 1280449532 3217 1280449532 4764 1280449532 4501 1280449532 3372 1280449533 4129 1280449533 6937 1280449533 6423 1280449533 4818 1280449534 4850 1280449534 8980 1280449534 8078 1280449534 6788 1280449535 5587 1280449535 10879 1280449535 9920 1280449535 8146 1280449536 6324 1280449536 12860 1280449536 11612 
like image 629
Sharat Chandra Avatar asked Jul 31 '10 03:07

Sharat Chandra


People also ask

How do you subtract multiple cells from a fixed number?

Select the cells from which you want to subtract the value. Right-click on the selected cells and then click on the Paste Special option. This will open the Paste Special window. In the Paste Special window, select Subtract and then click OK.

How do you subtract multiple numbers in a column?

Subtract a Number From Multiple Cells To subtract a number from a range of cells, click on the cell where you want to display the result, and enter “=” (equal) and the cell reference of the first number then “-” (minus) and the number you want to subtract.


1 Answers

What you have essentially works, you're just not outputting it. This will output what you want:

awk '{print ($1 - 1280449530) " " $2}' file 

You can also be slightly cleverer and not hardcode the shift amount:

awk '{        if(NR == 1) {            shift = $1        }         print ($1 - shift) " " $2 }' file  
like image 63
Michael Mrozek Avatar answered Oct 11 '22 21:10

Michael Mrozek