powernest
powernest

Reputation: 251

Vim substitution of fractional numbers

I have a huge file with the following format i.e fractional numbers with sign.

1.00, 4.000,-1.8765,0 56.456, -7.10, -6.65, 3.340, ... ...

I would like to change this format by inserting these numbers into signed[number] into say.

signed[-1.00], signed[4.000], signed[-1.8765], signed[0]

How can i do it vim?

Please suggest.

Upvotes: 0

Views: 69

Answers (3)

Kent
Kent

Reputation: 195269

this line would work for your example:

:%s/[0-9-.]\+/signed[&]/g

Upvotes: 1

FDinoff
FDinoff

Reputation: 31469

Something like this?

:%s/\v-?\d+(\.\d+)?/signed[&]/g

\v - turns on very magic mode.
-?\d+(\.\d+)? is look for any number. Negative sign and fractional part optional.
& - in the replacement part replaces it with the match.

1.00, 4.000,-1.8765,0 56.456, -7.10, -6.65, 3.340, ... ...

Turns into

signed[1.00], signed[4.000],signed[-1.8765],signed[0] signed[56.456], signed[-7.10], signed[-6.65], signed[3.340], ... ...

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96326

'<,'>s/[0-9][0-9.]*/signed[\0]/g

adjust the range for your needs.

Upvotes: 0

Related Questions