0

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.

Tharre
  • 111
  • 2
  • Do you want the lines replaced? do you want the numbers printed to the screen? – FDinoff Jul 22 '13 at 20:52
  • I don't care if they are replaced or just in a register, I just need them in this format somewhere. Replacing them would my preferred method however. – Tharre Jul 22 '13 at 21:02

2 Answers2

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
Birei
  • 35,723
  • 2
  • 77
  • 82
  • This works, however it seems to be far to much effort for such a simple task. – Tharre Jul 22 '13 at 21:01
  • 1
    @Tharre just take the `%s/\v^\D+(\d+\.\d*).*$/\1/` part – FDinoff Jul 22 '13 at 21:03
  • @Tharre: **FDinoff** is right. You can get rid of the `set backup` instruction because the original file is not modified. And the whole command creates a new file with the content you want. It doesn't seem too much code for that task. But from inside `vim` just do what **FDinoff** says. – Birei Jul 22 '13 at 21:08
1

try this:

%s/\v.{-}([0-9.]+).*/\1/
Kent
  • 189,393
  • 32
  • 233
  • 301