1

I have an R file of results, such as below:

filename run1 run2 run3
file1.py 109 103 100
file2.py 14123 14323 14333  
...
filen.py 2007 2130 2001

What is the easiest way to use VIM for adding decimal mark three places (from back, div 1000) to each number in each line?

Something like

filename run1 run2 run3
file1.py 0.109 0.103 0.100
file2.py 14.123 14.323 14.333  
...
filen.py 2.007 2.130 2.001

As @BLayer commented: Updated the example to make it more clear. For example numbers 1 and 10 should remain the same but three digit numbers like 100 should become 0.100.

4

here's the simplest way of doing it from my point of view:

:%s/\d\{3}\>/.&/g

Explanation

  • %s is here to search and replace

  • \d\{3} represents 3 digits
  • \> represents the end of a word (here the end of the number)

  • . is a dot
  • & repeats what you matched

As @BLayer noted, this solution can create numbers starting with a dot (e.g. ".123").
If That's not something you want, you can:

  • Exclude the 3 digits numbers from the search:
:%s/\d\zs\d\{3}\>/.&/g
  • Add a leading 0:
:%s/\D\zs\d\{3}\>/0.&/g
:%s/\d\zs\d\{3}\>/.&/g
3
  • 1
    Thanks. I tried to add dots on every number and then move it forward. This is cool. – Pushpa Apr 14 '20 at 11:25
  • 2
    Doesn't this lead to 3 digit numbers like 100 becoming .100? If that's not what you want then a pattern like \d\zs\d\{3}\> might be better. Or maybe you want that or you won't ever have 3-digit numbers...question isn't totally clear on that point. – B Layer Apr 15 '20 at 2:31
  • 1
    Yes. This is a possibility. And @zorzi has updated his answer. Thanks again. – Pushpa Apr 15 '20 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.