I would like to extract data from a document with VIM using regular expression but I only need the exact match and not the hole line. Basically just copy what :%s would replace.

Simple example:

<td>sell:8092.23</td>
<td>buy:850.00</td>
<td>sell:99.99</td>

... and extract the numbers:

8092.23
850.00
99.99

Is this possible in VIM and if how? Thanks in advance.

有帮助吗?

解决方案

try this:

%s/\v.{-}([0-9.]+).*/\1/

其他提示

Use a regular expression with anchors to the beginning and end of the line, do grouping to keep the numbers and replace the whole line with it. Run following command from the shell and it will create a new file output.txt with the numbers leaving the input file unmodified:

vim -u NONE -N -c '
    set backup |
    %s/\v^\D+(\d+\.\d*).*$/\1/ |
    saveas! output.txt |
    q!
' infile
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top