Perform a math operation on all numbers in a file

0

I have a list of pixel values in a file, and need each to be half its current value. The pattern for the value is always 123px where 123 varies drastically.

How can I half each pixel value?
I'm guessing sed will be helpful.

tbeseda

Posted 2011-05-13T17:28:06.230

Reputation: 101

Answers

1

I would suggest you use awk instead of sed.

If your input file file.dat contains one entry per line this will work:

$ awk '{ print $1/2"px"}' file.dat

If you need more sophisticated math use gawk instead of awk.

Benjamin Bannier

Posted 2011-05-13T17:28:06.230

Reputation: 13 999

Looking into the awk docs now, but each value has exists on a line with other info and another value, like this (it's CSS): .sprite-v16 { background-position: -462px -2002px; } – tbeseda – 2011-05-13T20:16:15.543

This is more complicated than your question made it seem. While looking around I came across http://www.issociate.de/board/post/461416/Math_in_sed/regexp_expression.html, but it needs some adijustments for your case. Since CSS can contain simple calculations you could just replace ps by *0.5px for the time being: sed 's/px/\*0\.5px/g'.

– Benjamin Bannier – 2011-05-14T13:50:14.213