Ubuntu – Use sed to use “find and replace” for each second occurrence

bashcommand linesedtext-editor

I have a folder with +1000 .dat files. And each file contains many lines of the following type:

-0.0999999999999659-0.0000000006287859
-0.08999999999997500.8000000006183942
-0.0799999999999841-0.0000000007463807
-0.06999999999999320.0000000008661516
-0.06000000000000230.0000000008640644
-0.05000000000001140.0000000008807621
-0.0400000000000205-0.7000000009575896
-0.02999999999997270.0000000009476864
-0.01999999999998180.0000000009150902
-0.00999999999999090.0000000008144152
0.00000000000000000.0000000007097434
0.00999999999999090.0000000007847500
0.01999999999998180.0000000009030998
0.03000000000002960.0000000009741985

For all the files I want to convert this to

-0.0999999999999659    -0.0000000006287859
-0.0899999999999750    0.8000000006183942
-0.0799999999999841    -0.0000000007463807
-0.0699999999999932    0.0000000008661516
-0.0600000000000023    0.0000000008640644
-0.0500000000000114    0.0000000008807621
-0.0400000000000205    -0.7000000009575896
-0.0299999999999727    0.0000000009476864
-0.0199999999999818    0.0000000009150902
-0.0099999999999909    0.0000000008144152
0.0000000000000000    0.0000000007097434
0.0099999999999909    0.0000000007847500
0.0199999999999818    0.0000000009030998
0.0300000000000296    0.0000000009741985

The only thing that is consistent in all these files that the second number (corresponding to the second dot on each line) is always smaller than 1.0 and greater than -1.0. But the first number can take any real value.

I therefore thought of using "find and replace" only for the second `dot' as follows. Find:

0.

Replace with:

   0.

I don't know how to specify sed only to act on the "second dot" on each line. Does anybody have a good idea on how to get this done?

Best Answer

 sed -E s'/(.*[^-])(-?0\.)/\1    \2/' 999.dat

The * is greedy and eats up as much characters as possible so the \. matches always the last one of the line. The [^-] ensures that the optional - of the second number gets into the second group.

Related Question