VI 删除除模式之外的所有内容 [英] VI delete everything except a pattern
本文介绍了VI 删除除模式之外的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个巨大的 JSON 输出,我只需要删除每行中除了一个小字符串之外的所有内容.
I have a huge JSON output and I just need to delete everything except a small string in each line.
字符串的格式为
"title": "someServerName"
someServerName"(引号内的部分)可能变化很大.
the "someServerName" (the section within quotes) can vary wildly.
我最近的一次是这样的:
The closest I've come is this:
:%s/\("title":\s"*"\)
但这只是设法删除
"title": "
我唯一想要在每一行中留下的是
The only thing I want left in each line is
"title": "someServerName"
编辑以回答发布的问题:
EDIT to answer the posted question:
我将要处理的文本的格式类似于
The Text I'm going to be working with will have a format similar to
{"_links": {"self": {"href": "/api/v2/servers/32", "title": "someServerName"},tons_of_other_json_crap_goes_here
我想要的最后是:
"title": "someServerName"
推荐答案
应该是 .*
而不是 *
来匹配一组任意字符.这可以完成工作:
It should be .*
rather than *
to match a group of any characters. This does the job:
%s/^.*\("title":\s".*"\).*$/\1/
各部分说明:
%s/
在每个匹配的行上替换.^.*
忽略从行首开始的任何字符.\("title":\s".*"\)
捕获标题和服务器名称.".*"
将匹配引号之间的任何字符..*$
忽略该行的其余部分./\1/
替换的结果将是第一个捕获的组.该组由括号\(...\)
捕获.
%s/
Substitute on each matching line.^.*
Ignore any characters starting from beginning of line.\("title":\s".*"\)
Capture the title and server name.".*"
will match any characters between quotes..*$
Ignore the rest of the line./\1/
The result of the substitution will be the first captured group. The group was captured by parentheses\(...\)
.
这篇关于VI 删除除模式之外的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文