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.

link|improve this question
feedback

1 Answer

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.

link|improve this answer
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 May 13 '11 at 20:16
This is more complicated than your question made it seem. While looking around I came across issociate.de/board/post/461416/Math_in_sed/…, 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'. – honk May 14 '11 at 13:50
feedback

Your Answer

 
or
required, but never shown

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