Question

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.

Était-ce utile?

La solution

try this:

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

Autres conseils

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top