Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .