0

I have a file that has a bunch of numbers ranging from .000000001 to 1000.

I'm trying to turn it into xml that looks something like this (where the number is multiplied by 10^7 in the name portion:

<TickTable Name="Fut_2500">
    <Tick Size="0.0025"/>
</TickTable>

I'm using the following awk command (in cygwin):

 awk '{print "<TickTable Name=\"Fut_" $0 * 1000000 "\">\n\t<Tick Size=\""$0 * 1 "\"/>\n</TickTable>"}' tick.txt

The formatting lookgs good, however, that results in the TickSize number being formatted with the 'e' notation such as 5e-07...:

<TickTable Name="Fut_0.5">
    <Tick Size="5e-07"/>
</TickTable>

So I tried running the same command, but without multiplying the second instance of the number by * 1:

awk '{print "<TickTable Name=\"Fut_" $0 * 1000000 "\">\n\t<Tick Size=\""$0 "\"/>\n</TickTable>"}' tick.txt

But now, the formatting becomes all messed up, where if I > to a file, it looks like:

<TickTable Name="Fut_0.5">
    <Tick Size=".0000005
"/>

and looks like this in cygwin:

<TickTable Name="Fut_5">
"/>     <Tick Size=".000005
</TickTable>

Why does this happen? How can I fix this? (Easiest way is probably to stop the auto-formatting of the decimals in 'e' notation?

oguz ismail
  • 1
  • 16
  • 47
  • 69
LazyCubicleMonkey
  • 1,213
  • 10
  • 17
  • 1
    Learn about [mcve] s. Can't you boil this down to something more like `echo .000000001 | awk '{print $0*1000000 }'` ? There doesn't seem to be any reason that the XML formatting needs to be in your Q. Good luck. – shellter Jan 07 '21 at 17:18
  • 1
    Ah, cygwin and embedded newlines. Try `dos2unx tick.txt` and rerun. Also read up about `printf` and the `%[n.m]f` option (just search for `%f`). You should be able to find that on [awk tutorial](https://grymoire.com/Unix/Awk.html) . Good luck. – shellter Jan 07 '21 at 18:08
  • That worked! Converting file to unix format fixed it. Thank you! – LazyCubicleMonkey Jan 07 '21 at 18:18
  • 2
    Glad that helped. you'll do well to read, review and take to heart the items on this page : https://stackoverflow.com/tags/bash/info . Also note the sections labeled "Before asking about Problematic code" and "How to turn a bad script into a good question" .Good luck. – shellter Jan 07 '21 at 18:21

0 Answers0