0

The in file looks like this

-17.3644       0.00000000     0.00000000 ....
-17.2703       0.00000000     0.00000000 ....
-17.1761       0.00000000     0.00000000 ....
-16.5173       0.00000000     0.00000000 ....
-16.4232       0.00000000     0.00000000 ....

The desire output should be

-173.644       0.00000000     0.00000000 ....
-172.703       0.00000000     0.00000000 ....
-171.761       0.00000000     0.00000000 ....
-165.173       0.00000000     0.00000000 ....
-164.232       0.00000000     0.00000000 ....

so I want to multiply let's say 1st column by 10 but on the same time also keep the other 1000 columns. with awk '{print $1*10}' infile > outfile you only print first column, how can I also keep the other columns?

2

try

awk '{$1=$1*10 ; print }'

this will change first parameter, and print whole line.

to keep formating to 3 digit, use

awk '{$1=sprintf("%.3f",$1*10);print;}' 
  • to add on this, if someone is interested in the format as well awk '{$1=sprintf("%.3f",$1*10);print}' – Dimitris Mintis Jul 16 '19 at 14:42
  • tanks, I update with your comment. – Archemar Jul 16 '19 at 14:48
0

Tested with python

#!/usr/bin/python
import re
k=open('p.txt','r')
for i in k:
    k=re.sub(' +',' ',i).split(' ')
    print float(k[0])*10,k[1],k[2]

output

-173.644 0.00000000 0.00000000
-172.703 0.00000000 0.00000000
-171.761 0.00000000 0.00000000
-165.173 0.00000000 0.00000000
-164.232 0.00000000 0.00000000
  • 1
    What if there are 1000 columns? – Prvt_Yadav Jul 16 '19 at 18:19
0
$ < infile tr \- _ |
  dc -e "
    [q]sq
    [SMlN1+sNz1<a]sa
    [LMn32anlN1-dsN0<b]sb
    [?z0=q0sNlax.1/n32anlbx[]pclcx]sc
    3klcx
  "

Output:

-173.644 0 0 0 0 0 0 
-172.703 0 0 0 0 0 0 
-171.761 0 0 0 0 0 0 
-165.173 0 0 0 0 0 0 
-164.232 0 0 0 0 0 0

Brief Explanation:

The dc is a reverse Polish notation RPN calculator which stores data on it's main stack. Store in a named stack M the fields of the current line till you reach only one standing. Divide it .1 == multiply by 10 AND get scaling applied on the result. Now regurgitate the stack M and place back on main stack the current line fields which are left unmodified. Keep printing them to stdout at the same time we are popping them off the M stack. At the end of file or upon reaching an empty/blank line, whichever happens first, we quit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.