Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have tried several things but got no luck. I have an input file that has a lot of fields like this one

weight=0.123456

mixed with other fields. I want to multiply all such weights by a factor, say 100, so I want to see

weight=12.3456

in the output file. What's a good way to do that using sed or awk?

share|improve this question
    
"mixed with other fields" is too vague to be able to script something useful in awk (precisely!). Anyway, the general approach is: a) split the input in fields, trim the 'weight=' part; b) feed bc with that; c) printf what you need –  dawud Apr 12 '13 at 19:01
    
not even bc, GNU awk supports arithmetic operations –  dawud Apr 12 '13 at 19:02

2 Answers 2

For a simple file containing


$ cat input
weight=0.0012
weight=0.0345

$ awk '/weight/{gsub(/weight=/, "")};{ value=$0*100; printf "weight=%s\n", value}' input

does the trick. More complicated input files need more complicated rules (possibly).

share|improve this answer
awk -F= '$1 ~ /weight/{print "weight="$2*100}' file
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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