如何通过 sed 命令更改 KML 文件中的浮点精度而不更改其结构

How to change float precision inside a KML file via sed commands without changing its structure

我有一个带有浮点数和字符串的 kml 文件(下面是简短的示例,真正的文件大了数百行)。我想仅使用 SED 或 AWK 命令更改浮点数的精度(例如,小数点后 4 位数字),而不触及文件本身的结构和其余字符串。我试过 awk、grep 和 sed,但最终只选择了数字。有人可以帮我完成这项任务吗?

        <Placemark>
                <name>Line 276</name>
                <styleUrl>#st-9324-0</styleUrl>
                <LineString>
                    <tessellate>1</tessellate>
                    <coordinates>
                        -82.0843550361,-10.3436718895,0
                        -82.0733687247,-10.3414798573,0
                        -82.067875569,-10.3436762828,0
                        -82.0623824134,-10.3464215529,0
                        -82.051396102,-10.354657111,0
                        -82.0513917085,-10.3546626018,0
                        -82.0470007396,-10.3656489207,0
                        -82.0470007396,-10.3766352397,0
                        -82.0491996781,-10.3876215586,0
                        -82.051396102,-10.3912813368,0
                        -82.0568892577,-10.3986078776,0
                        -82.0623824134,-10.4030032431,0
                        -82.0706234567,-10.4095941965,0
                        -82.0733687247,-10.4114246674,0
                        -82.0843550361,-10.4169172451,0
                        -82.0953303765,-10.4205805155,0
                        -82.0953413474,-10.4205832616,0
                        -82.1063276588,-10.4219531504,0
                        -82.1173139701,-10.4219531504,0
                        -82.1209737457,-10.4205805155,0
                        -82.1283002815,-10.41618515,0
                        -82.1337934371,-10.4095941965,0
                        -82.1345777463,-10.3986078776,0
                        -82.1324208031,-10.3876215586,0
                        -82.1283002815,-10.3793805097,0
                        -82.1261038575,-10.3766352397,0
                        -82.1173139701,-10.3678453462,0
                        -82.1151175462,-10.3656489207,0
                        -82.1063276588,-10.3583258722,0
                        -82.1008345031,-10.3546626018,0
                        -82.0953413474,-10.3502672363,0
                        -82.0843623548,-10.3436762828,0
                        -82.0843550361,-10.3436718895,0
                    </coordinates>
                </LineString>
            </Placemark>                    
                    <Placemark>
                            <name>12</name>
                            <styleUrl>#st-9325-0</styleUrl>
                            <Point>
                                    <coordinates>
                                    -80.881497121,-43.7490060037,0
                                    </coordinates>
                           </Point>
                    </Placemark>

谢谢。

jb

我会使用以下 sed 命令:

s/([0-9]+\.[0-9]{4})[0-9]+//g

它匹配:

  • 进入第一组:
    • 尽可能多的数字
    • 一个文字点
    • 还有四位数
  • 群外:
    • 尽可能多的其他数字

然后用第一组替换整个匹配项,截断多余的数字。

使用 GNU sed,使用 -i 编辑文件 "in-place" 和 -r 以避免必须转义括号和 +sed -ri 's/([0-9]+\.[0-9]{4})[0-9]+//g' file

编辑:实际上我们不需要匹配符号