Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed calculations

Tags:

regex

sed

I want to parse a css file and multiply each pixel value by (2/3). I was wondering if this was possible with sed? I know this is incorrect syntax but i think it'll bring home the explanation of what i want to achieve:

sed -e "s|\([0-9]*\)px|int((\1 * 2)/3)|g" file.css

So basically I want to take \1, multiply it by (2/3) and cast to and int. Or maybe it's more possible with awk? Suppose I could write a python script, but would like to know if it can be done by quicker means.

Thanks

like image 307
tomh Avatar asked Dec 30 '22 00:12

tomh


1 Answers

use awk

$ cat file
foo: 3px; bar: 6px

$ awk '{for(i=1;i<=NF;i++){if($i~/^[0-9]+px/){o=$i;sub(/^[0-9]+/,"",o);$i=($i+0)*(2/3)o}}}1' file
foo: 2px; bar: 4px
like image 61
ghostdog74 Avatar answered Dec 31 '22 12:12

ghostdog74