使用 Vim 将正则表达式搜索的结果保存到文件中

发布于 2024-07-25 01:32:07 字数 149 浏览 5 评论 0原文

我有一个 HTML 文件,我想获取该文件中的所有链接并使用 Vim 将其保存到另一个文件中。

我知道正则表达式会是这样的:

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

但我不知道从这里去哪里。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

美人迟暮 2024-08-01 01:32:08

杰夫·肉丸·杨 (Jeff Meatball Yang) 就快到了。

正如 Sasha 所写,如果您使用 w ,它将完整的原始文件写入输出文件

要仅写入匹配的行,您必须添加“。” 在“w”之前:

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

请注意,输出文件需要存在。

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.

冷情 2024-08-01 01:32:08

清除reg:x

qxq

搜索regex(无论什么)并附加到reg:x

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

打开一个新选项卡

:tabnew outfile

放入reg:x

"xp

写入文件

:w

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
慕烟庭风 2024-08-01 01:32:08

这里的挑战在于提取可能有多个在线的所有链接,否则您可以简单地执行以下操作:

" Extract all lines with href=
:g/href="[^"]\+"/w >> list_of_links.txt
" Open the new file
:e list_of_links.txt
" Extract the bit inside the quotation marks
:%s/.*href="\([^"]\+\)".*/\1/

最简单的方法可能是这样做:

" Save as a new file name
:saveas list_of_links.txt
" Get rid of any lines without href=
:g!/href="\([^"]\+\)"/d
" Break up the lines wherever there is a 'href='
:%s/href=/\rhref=/g
" Tidy up by removing everything but the bit we want
:%s/^.*href="\([^"]\+\)".*$/\1/

或者(遵循类似的主题),

:g/href="[^"]\+"/w >> list_of_links.txt
:e list_of_links.txt
:%s/href=/\rhref=/g
:%s/^.*href="\([^"]\+\)".&$/\1/

(请参阅:help saveas, :help :vglobal, :help :s)

但是,如果您确实想以更直接的方式执行此操作,您可以执行以下操作:

" Initialise register 'h'
:let @h = ""
" For each line containing href=..., get the line, and carry out a global search
" and replace that extracts just the URLs and a double quote (as a delimiter)
:g/href="[^"]\+"/let @h .= substitute(getline('.'), '.\{-}href="\([^"]\+\)".\{-}\ze\(href=\|$\)', '\1"', 'g')
" Create a new file
:new
" Paste the contents of register h (entered in normal mode)
"hp
" Replace all double quotes with new-lines
:s/"/\r/g
" Save
:w

最后,您可以在带有 for 循环的函数中执行此操作,但我就留给别人写吧!

The challenge here lies with extracting all of the links where there may be multiple on line, otherwise you'd be able to simply do:

" Extract all lines with href=
:g/href="[^"]\+"/w >> list_of_links.txt
" Open the new file
:e list_of_links.txt
" Extract the bit inside the quotation marks
:%s/.*href="\([^"]\+\)".*/\1/

The simplest approach would probably be to do this:

" Save as a new file name
:saveas list_of_links.txt
" Get rid of any lines without href=
:g!/href="\([^"]\+\)"/d
" Break up the lines wherever there is a 'href='
:%s/href=/\rhref=/g
" Tidy up by removing everything but the bit we want
:%s/^.*href="\([^"]\+\)".*$/\1/

Alternatively (following a similar theme),

:g/href="[^"]\+"/w >> list_of_links.txt
:e list_of_links.txt
:%s/href=/\rhref=/g
:%s/^.*href="\([^"]\+\)".&$/\1/

(see :help saveas, :help :vglobal, :help :s)

However, if you really wanted to do it in a more direct way, you could do something like this:

" Initialise register 'h'
:let @h = ""
" For each line containing href=..., get the line, and carry out a global search
" and replace that extracts just the URLs and a double quote (as a delimiter)
:g/href="[^"]\+"/let @h .= substitute(getline('.'), '.\{-}href="\([^"]\+\)".\{-}\ze\(href=\|$\)', '\1"', 'g')
" Create a new file
:new
" Paste the contents of register h (entered in normal mode)
"hp
" Replace all double quotes with new-lines
:s/"/\r/g
" Save
:w

Finally, you could do it in a function with a for loop, but I'll leave that for someone else to write!

天涯沦落人 2024-08-01 01:32:08

将光标放在第一行/第一列,然后尝试以下操作:

:redir > output.txt|while search('href="', "We")|exe 'normal yi"'|echo @"|endwhile|redir END

Put your cursor in the first row/column and try this:

:redir > output.txt|while search('href="', "We")|exe 'normal yi"'|echo @"|endwhile|redir END
美煞众生 2024-08-01 01:32:08

你试过这个吗?

:g/href="\v([a-z_/]+)"/w>> 输出文件

Have you tried this?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文