Regex find-and-replace in vim: adding .0 to numbers

I have a file that looks below.

1   1  0  #  1 
6   1  0  #  2 
8   1  0  #  3 
10  1  0  #  4 
12  1  0  #  6 

How can I add .0 to all numbers except the numbers behind #. I think this should not be too hard to do with regular expressions, but my knowledge of regular expressions is too rusty.

+3
source share
4 answers

If your numbers after #do not have spaces after them, you can use:

:g/\([0-9]\+\) /s//\1.0 /g

Using ()creates groups that you can name \Din the replacement text, where Dis the position of the group in the search bar. This will give you:

1.0   1.0  0.0  #  1
6.0   1.0  0.0  #  2
8.0   1.0  0.0  #  3
10.0  1.0  0.0  #  4
12.0  1.0  0.0  #  6

If they have spaces after them (which looks like you have), you will get:

1.0   1.0  0.0  #  1.0
6.0   1.0  0.0  #  2.0
8.0   1.0  0.0  #  3.0
10.0  1.0  0.0  #  4.0
12.0  1.0  0.0  #  6.0

in this case you can:

:g/\.0\(  *\)$/s//\1/g

.

+7

VIM:

:%s/\v(#.*)@<!\d+/&.0/g

: \v= (. help \v), @<! , (. help \@<!). 1 , .0.

+8

, , sed, .

EDIT: , vim , .

sed 's/\([0-9]\+\) \+\([0-9]\+\) \+\([0-9]\+\)/\1.0 \2.0 \3.0/' file
+1

sed 's/[0-9]/&.0/g' | sed 's/.0 [] * $//g'

sed add.0 .0

0

Source: https://habr.com/ru/post/1756517/


All Articles