0

I have a file which contains so many values which ends with a string px. I need to find all those values, multiple them with 0.43 and again write them to the file with the substring pt instead of px How do I achieve this ??

eg string from the file:

 .ClassOne
 {
 margin: .35em 5px;
 overflow: hidden;
 padding: 0 1.4em 0 .5em;
 position: relative;
 text-overflow: ellipsis;
 white-space: nowrap;
}
.ClassTwo
{
 border: 7px solid #FFF;
 border-top-color: #000;
 position: absolute;
 right: 6px;
 top: .95em;
}
.ClassThree
{
 bottom: 5px;
 height: 1px;
 left: 1em;
 position: absolute;
 right: 2em;
}

To find all the values that ends with px, and convert them to points by multiplying with 0.43 and write them back to the file. Example: 5px = 2.15pt, -9px = -3.87pt

How to achieve this using a single line command line in unix?? Since I am new to shell scripting, I need some help. All I know is that it can be achieved using sed -i.

4

1 回答 1

2

使用awkonliner,您可以这样做:

awk '{for (i=1;i<=NF;i++) if ($i~/px/) {$i=$i*0.43"pt";if (i=NF) $i=$i";"}}1' file
.ClassOne
 {
margin: .35em 2.15pt;
 overflow: hidden;
 padding: 0 1.4em 0 .5em;
 position: relative;
 text-overflow: ellipsis;
 white-space: nowrap;
}
.ClassTwo
{
border: 3.01pt solid #FFF;;
 border-top-color: #000;
 position: absolute;
right: 2.58pt;
 top: .95em;
}
.ClassThree
{
bottom: 2.15pt;
height: 0.43pt;
 left: 1em;
 position: absolute;
 right: 2em;
}

这将所有字段与pxint 相乘,然后将其0.43相加pt
,然后将数据写回文件。

于 2013-11-11T08:04:54.290 回答