Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving results of Regex Search into a file with Vim

I've got a HTML file, and I'd like to grab all the links that are in the file and save it into another file using Vim.

I know that the regex would be something like:

:g/href="\v([a-z_/]+)"/

but I don't know where to go from here.

like image 292
Sasha Avatar asked Nov 29 '22 20:11

Sasha


2 Answers

Jeff Meatball Yang was almost there.

As Sasha wrote if you use w it writes the full original file to the outfile

To only write the matched line, you have to add '.' before 'w':

:g/href="\v([a-z_/]+)"/ .w >> outfile

Note that the outfile needs to exists.

like image 88
Jean-christophe Zulian Avatar answered Dec 04 '22 22:12

Jean-christophe Zulian


clear reg:x

qxq

search regex(whatever) and append to reg:x

:g/regex/call setreg('X', matchstr(getline('.'), 'regex') . "\n")

open a new tab

:tabnew outfile

put reg:x

"xp

write file

:w
like image 25
kev Avatar answered Dec 04 '22 22:12

kev