When parsing log files, I often use :g!/<string I want to keep in the log>/d
to remove all lines from the log that don't contain a string of interest. Well, now I'd like to use this powerful command to replace all strings that don't match with an empty line.
在解析日志文件时,我经常使用:g!/
I can find little to no information on :g!
, other than the command that I already use, and that the format is :g!/<match string>/<cmd>
, where cmd
is an "ex command", or one of the : commands used in vim.
我几乎找不到关于:g!,而不是我已经使用的命令,格式是:g!/ <匹配字符串> /
So I thought it might be possible to use the s
command to do this, but my understanding is limited in this area. Can anyone suggest how to format the command properly? Or are there other tools better suited for this task? (sed / awk? I haven't really ever used those but know they are pretty powerful).
所以我认为使用s命令来做这个是可能的,但是我的理解在这方面是有限的。有人能建议如何正确格式化命令吗?或者还有其他更适合这个任务的工具吗?(sed、awk吗?我从来没有用过,但我知道它们很强大)。
The other option is to just punt and write a utility in Python to do it for me.
另一种选择是用Python编写一个实用程序来帮我完成。
2 个解决方案
#1
12
First of all, you can use :v
as a shortcut for :g!
. The etymology comes from the -v
option to the standard grep command, I think.
首先,你可以用:v作为:g的快捷方式。词源来自于grep命令的-v选项。
Yes, you can use any ex command, including :s
. By default, :s
acts on a single line; combining it with :g
(or :g!
or :v
) you can select the lines on which it acts. If I understand correctly, then
是的,你可以使用任何ex命令,包括:s。默认情况下:s作用于一行;与:g(或:g!或者:v)你可以选择它的作用线。如果我理解正确,那么
:v/<string I want to keep>/s/.*//
is exactly what you want. You will probably want to follow up with :noh
.
这正是你想要的。你可能想接着说:不。
Addendum: I am a little surprised at how popular this short answer has been. A funny thing is that the docs (:help :g
and then scroll down a bit) mention :%s/pat/PAT/g
as an alternative ("two characters shorter!") to :g/pat/s//PAT/g
. I think this means that :g/pat/s/
used to be more familiar than :%s/pat/
.
附录:我对这个简短的回答如此受欢迎感到有点惊讶。有趣的是,文档(:帮助:g然后向下滚动一点)提到:%s/pat/ pat/ g作为替代(“两个字符更短!”)到:g/pat/s/ pat/ g。我认为这意味着:g/pat/s/曾经比:%s/pat/更熟悉。
Shameless plug: for more on :g
and :s
, you might also be interested in my answer to why :%s/^$//g is not equal to :g/^$/d in vim?
无耻的插头:更多:g和年代,你可能也会感兴趣我的回答为什么:% s / ^ $ / / g不等于:g / ^ $ / d在vim中?
#2
0
On linux you might prefer:
在linux上,您可能更喜欢:
:%!grep STRING_YOU_WANT_TO_KEEP
#1
12
First of all, you can use :v
as a shortcut for :g!
. The etymology comes from the -v
option to the standard grep command, I think.
首先,你可以用:v作为:g的快捷方式。词源来自于grep命令的-v选项。
Yes, you can use any ex command, including :s
. By default, :s
acts on a single line; combining it with :g
(or :g!
or :v
) you can select the lines on which it acts. If I understand correctly, then
是的,你可以使用任何ex命令,包括:s。默认情况下:s作用于一行;与:g(或:g!或者:v)你可以选择它的作用线。如果我理解正确,那么
:v/<string I want to keep>/s/.*//
is exactly what you want. You will probably want to follow up with :noh
.
这正是你想要的。你可能想接着说:不。
Addendum: I am a little surprised at how popular this short answer has been. A funny thing is that the docs (:help :g
and then scroll down a bit) mention :%s/pat/PAT/g
as an alternative ("two characters shorter!") to :g/pat/s//PAT/g
. I think this means that :g/pat/s/
used to be more familiar than :%s/pat/
.
附录:我对这个简短的回答如此受欢迎感到有点惊讶。有趣的是,文档(:帮助:g然后向下滚动一点)提到:%s/pat/ pat/ g作为替代(“两个字符更短!”)到:g/pat/s/ pat/ g。我认为这意味着:g/pat/s/曾经比:%s/pat/更熟悉。
Shameless plug: for more on :g
and :s
, you might also be interested in my answer to why :%s/^$//g is not equal to :g/^$/d in vim?
无耻的插头:更多:g和年代,你可能也会感兴趣我的回答为什么:% s / ^ $ / / g不等于:g / ^ $ / d在vim中?
#2
0
On linux you might prefer:
在linux上,您可能更喜欢:
:%!grep STRING_YOU_WANT_TO_KEEP