Domanda

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.

È stato utile?

Soluzione

try this:

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

Altri suggerimenti

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top