Reputation: 45
I have a file with the following contents:
set x 0.00456 y 0.05896.
I want to multiply the digits by a fixed amount (lets say 1000). The numbers do not always exist in the same column so anything with awk is out of the picture. I have been trying this but not sure if the way I am using submatch is correct.
%s/ \d*\.\d*/\=submatch(2)*100
Upvotes: 2
Views: 434
Reputation: 195039
Your submatch(2)
usage is not correct. You don't have any matching groups, so you should use submatch(0)
.
Another problem in your codes is, you should first change the string into float, then do the calculation:
%s/\v\d+[.]\d+/\=str2float(submatch(0))*1000/g
The numbers do not always exist in the same column so anything with awk is out of the picture.
This is not true. You can check each column, if it matches the number format, you do the math calculation:
awk '{for(i=1;i<=NF;i++)if($i~/[0-9]+[.][0-9]+/)$i*=1000}7' file
You can also call the awk within your vim:
%!awk '{for(i=1;i<=NF;i++)if($i~/[0-9]+[.][0-9]+/)$i*=1000}7'
Upvotes: 6