Issue
I have a text file with 30 colums. I want to shift column number one by a small amount, let's say by 0.01, and want to keep all the other columns unchanged. An example of a three-row and thirtee column file is shown below:
-0.4122 50.0000 59.19254908 0.20390966E+21 0.50210854E+04 0.10024552E+05 0.50210854E+04 0.20390966E+21 0.12781341E+05 0.10024552E+05 0.12781341E+05 0.17430806E+21 -0.13971462E-04 -0.34940712E-21 -0.84283159E-21 -0.34940712E-21 -0.13971462E-04 -0.10333849E-20 0.73796435E-22 0.12933410E-21 -0.30127810E-04 0.22653580E+15 0.43388767E-02 0.12519835E-01 0.43388767E-02 0.22653580E+15 0.15837399E-01 0.12519835E-01 0.15837399E-01 0.21852932E+15
-0.4235 100.0000 59.19127436 0.19338258E+21 0.41823435E+04 0.10186550E+05 0.41823435E+04 0.19338258E+21 0.12893514E+05 0.10186550E+05 0.12893514E+05 0.17755002E+21 -0.90933331E-05 -0.21562030E-21 -0.62836171E-21 -0.21562030E-21 -0.90933331E-05 -0.61018803E-21 -0.48935277E-22 0.10805572E-21 -0.19116589E-04 0.44412500E+15 0.74492273E-02 0.26238884E-01 0.74492273E-02 0.44412500E+15 0.32779761E-01 0.26238884E-01 0.32779761E-01 0.46142213E+15
-0.4620 150.0000 59.19108585 0.18970962E+21 0.38138923E+04 0.10378024E+05 0.38138923E+04 0.18970962E+21 0.13078457E+05 0.10378024E+05 0.13078457E+05 0.18150561E+21 -0.70829346E-05 -0.23441645E-21 -0.40816670E-21 -0.23441645E-21 -0.70829346E-05 -0.31840202E-21 -0.91970004E-22 0.71596380E-22 -0.12540327E-04 0.68630757E+15 0.12171742E-01 0.40052678E-01 0.12171742E-01 0.68630757E+15 0.50206970E-01 0.40052678E-01 0.50206970E-01 0.71030708E+15
With this text file, the output should be as
-0.4222 50.0000 59.19254908 0.20390966E+21 0.50210854E+04 0.10024552E+05 0.50210854E+04 0.20390966E+21 0.12781341E+05 0.10024552E+05 0.12781341E+05 0.17430806E+21 -0.13971462E-04 -0.34940712E-21 -0.84283159E-21 -0.34940712E-21 -0.13971462E-04 -0.10333849E-20 0.73796435E-22 0.12933410E-21 -0.30127810E-04 0.22653580E+15 0.43388767E-02 0.12519835E-01 0.43388767E-02 0.22653580E+15 0.15837399E-01
-0.4335 100.0000 59.19127436 0.19338258E+21 0.41823435E+04 0.10186550E+05 0.41823435E+04 0.19338258E+21 0.12893514E+05 0.10186550E+05 0.12893514E+05 0.17755002E+21 -0.90933331E-05 -0.21562030E-21 -0.62836171E-21 -0.21562030E-21 -0.90933331E-05 -0.61018803E-21 -0.48935277E-22 0.10805572E-21 -0.19116589E-04 0.44412500E+15 0.74492273E-02 0.26238884E-01 0.74492273E-02 0.44412500E+15 0.32779761E-01
-0.472 150.0000 59.19108585 0.18970962E+21 0.38138923E+04 0.10378024E+05 0.38138923E+04 0.18970962E+21 0.13078457E+05 0.10378024E+05 0.13078457E+05 0.18150561E+21 -0.70829346E-05 -0.23441645E-21 -0.40816670E-21 -0.23441645E-21 -0.70829346E-05 -0.31840202E-21 -0.91970004E-22 0.71596380E-22 -0.12540327E-04 0.68630757E+15 0.12171742E-01 0.40052678E-01 0.12171742E-01 0.68630757E+15 0.50206970E-01
I used the below script to do this operation but I am wondering if a simple operation is there.
cat test.dat | awk '{print $1-0.01, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27}'
After the operation, the formatting of the file should not change. For example, if I have the number 10.0000 in a column and after subtracting 10 (it may be another number too), it should result in 0.0000 not only 0.
Solution
Something like that?
awk '{$1 -= 0.01; print}' data.txt
If you want to save first column formatting:
awk '{$1 = sprintf("%0.4f", $1 - 0.01); print}' data.txt
And for "table" appearance:
awk '{$1 = sprintf("%0.4f", $1 - 0.01); print}' data.txt | column --table
Answered By - Arnaud Valmary Answer Checked By - Robin (WPSolving Admin)