linux - Parsing XML - Subtracting values from tags -
linux - Parsing XML - Subtracting values from tags -
i have next xml construction in big file:
<sit>619709.6044;144998.7059;-090372.58119</sit> <vll>0;0;0</vll> <cor>255;0;255</cor> how subtract values in sit tag? tag comprised of 3 values separated ; each has subtracted specific number.
any unix tool can used. (awk,sed,bc,etc)
so if specific number 1000 first value, 100 sec value , 10 3rd value result be:
<sit>618709;144898;-090362</sit> <vll>0;0;0</vll> <cor>255;0;255</cor> no need maintain fractions.
here's 1 way using awk. run like:
awk -v a=1000 -v b=100 -v c=10 -f "[<;>]" -v ofs=";" -f ./script.awk file contents of script.awk:
/^<sit>/ && /<\/sit>$/ { $0 = "<sit>" format($3, a) ofs format($4, b) ofs format($5, c) "</sit>" }1 function format(field, var) { f = sub(/^-/, "", field) homecoming (f == 1 ? "-" : "") sprintf("%06d", int(field-var)) } results:
<sit>618709;144898;-090362</sit> <vll>0;0;0</vll> <cor>255;0;255</cor> you haven't been exclusively clear how format output. looks want integers padded 6 leading zeros, regardless of whether positive or negative. above script that. if 0 typo, one-liner should suffice:
awk -v a=1000 -v b=100 -v c=10 -f "[<;>]" -v ofs=";" '/^<sit>/ && /<\/sit>$/ { $0 = "<sit>" sprintf("%06d",int($3-a)) ofs sprintf("%06d",int($4-b)) ofs sprintf("%06d",int($5-c)) "</sit>" }1' file results:
<sit>618709;144898;-90382</sit> <vll>0;0;0</vll> <cor>255;0;255</cor> linux xml-parsing sed awk bc
Comments
Post a Comment