使用 Vim 将正则表达式搜索的结果保存到文件中
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
杰夫·肉丸·杨 (Jeff Meatball Yang) 就快到了。
正如 Sasha 所写,如果您使用 w ,它将完整的原始文件写入输出文件
要仅写入匹配的行,您必须添加“。” 在“w”之前:
请注意,输出文件需要存在。
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':
Note that the outfile needs to exists.
清除reg:x
搜索
regex
(无论什么)并附加到reg:x打开一个新选项卡
放入reg:x
写入文件
clear reg:x
search
regex
(whatever) and append to reg:xopen a new tab
put reg:x
write file
这里的挑战在于提取可能有多个在线的所有链接,否则您可以简单地执行以下操作:
最简单的方法可能是这样做:
或者(遵循类似的主题),
(请参阅:help saveas, :help :vglobal, :help :s)
但是,如果您确实想以更直接的方式执行此操作,您可以执行以下操作:
最后,您可以在带有 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:
The simplest approach would probably be to do this:
Alternatively (following a similar theme),
(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:
Finally, you could do it in a function with a for loop, but I'll leave that for someone else to write!
将光标放在第一行/第一列,然后尝试以下操作:
Put your cursor in the first row/column and try this:
你试过这个吗?
:g/href="\v([a-z_/]+)"/w>> 输出文件
Have you tried this?
:g/href="\v([a-z_/]+)"/w >> outfile