Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want an alternative solution to this jQuery hack, tasting a bit reinventing the wheel -- look I am sure I could do this with some one-liner using basic *ix tools. Look much easier and straightforward way is to find all numbers appended with px, some \d+px -match and then multiply it with a constant but \d will fail -- match only if not prepended by a digit [^\d]\d+px and then do the replacement/multiplication.

The simplest and the most elegant solution wins, how can you do the match-multiply-replace?

I use Vim but open to new ideas.

share|improve this question
add comment

1 Answer

bash-4.2$ cat map.htm 
<map name="world">
  <area shape="rect" coords="0%,0%,32%,32%" >
  <area shape="rect" coords="8%,40%,50%,80%" >
  <area shape="rect" coords="56%,4%,84%,18%" >
  <area shape="rect" coords="74%,20%,90%,40%" >
  <area shape="rect" coords="70%,42%,96%,62%" >
  <area shape="rect" coords="0%,74%,70%,95%" >
</map>

bash-4.2$ perl -pe '$w=640;$h=480;s!(\d+)%,(\d+)%,(\d+)%,(\d+)%!($1*$w/100).",".($2*$h/100).",".($3*$w/100).",".($4*$h/100)!ge' map.htm 
<map name="world">
  <area shape="rect" coords="0,0,204.8,153.6" >
  <area shape="rect" coords="51.2,192,320,384" >
  <area shape="rect" coords="358.4,19.2,537.6,86.4" >
  <area shape="rect" coords="473.6,96,576,192" >
  <area shape="rect" coords="448,201.6,614.4,297.6" >
  <area shape="rect" coords="0,355.2,448,456" >
</map>

Of course this will fail in certain circumstances, because regular expressions are not the way to parse HTML, as concluded on SO. But solving with a dedicated HTML parser would not be a one-liner anymore.

But if you are sure there will be no conflicts with coordinate pair-looking plain text, you can even reduce it to this:

perl -pe '$w=640;$h=480;s!(\d+)%,(\d+)%!int($1*$w/100).",".int($2*$h/100)!ge' map.htm
share|improve this answer
add comment

Your Answer

 
discard

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