使用vim中的regex替换字符和换行符

时间:2014-07-08 08:26:32

标签: regex vim replace

我有以下字符串

Local intf     Local circuit              Dest address    VC ID      Status
-------------  -------------------------- --------------- ---------- ----------
Gi36/1         Eth VLAN 3018              181.181.181.181 3018       UP
10.65.220.180#
--- 19:58:22 ---
482: linuxserver: 2014-07-07T19:58:22: %framework-5-NOTICE: %[pname=TRP-__taskid1]: id: testcase_info_id37
483: linuxserver: 2014-07-07T19:58:22: %framework-5-NOTICE: %[pname=TRP-__taskid1]: starttime: 2014-07-07 19:58:22
484: linuxserver: 2014-07-07T19:58:22: %framework-5-NOTICE: %[pname=TRP-__taskid1]: name: testcase_info_id37
485: linuxserver: 2014-07-07T19:58:22: %framework-5-NOTICE: %[pname=TRP-__taskid1]: Starting execution of subtest testcase_info_id37
+++ 19:58:22 +++
Local intf     Local circuit              Dest address    VC ID      Status
-------------  -------------------------- --------------- ---------- ----------
Gi36/1         Eth VLAN 3018              181.181.181.181 3018       UP

我想替换包含" linuxserver"等字符的所有行。在它。

我在下面的vi中尝试过。

:%s/.*linuxserver.*//g

但是,在更换之后,我的输出为

    Local intf     Local circuit              Dest address    VC ID      Status
    -------------  -------------------------- --------------- ---------- ----------
    Gi36/1         Eth VLAN 3018              181.181.181.181 3018       UP
    10.65.220.180#
    --- 19:58:22 ---
    //A new line here
    //A new line here
    //A new line here    
    //A new line here    
    +++ 19:58:22 +++
    Local intf     Local circuit              Dest address    VC ID      Status
    -------------  -------------------------- --------------- ---------- ----------
    Gi36/1         Eth VLAN 3018              181.181.181.181 3018       UP

我希望它如下所示,

Local intf     Local circuit              Dest address    VC ID      Status
    -------------  -------------------------- --------------- ---------- ----------
    Gi36/1         Eth VLAN 3018              181.181.181.181 3018       UP
    10.65.220.180#
    --- 19:58:22 ---
    +++ 19:58:22 +++
    Local intf     Local circuit              Dest address    VC ID      Status
    -------------  -------------------------- --------------- ---------- ----------
    Gi36/1         Eth VLAN 3018              181.181.181.181 3018       UP

我怎么能做到这一点?提前谢谢。

2 个答案:

答案 0 :(得分:4)

尝试:

:g/linuxserver/d

:help global

了解更多信息。

答案 1 :(得分:4)

:global命令最适合您的问题(:g/linuxserver/d),但这就是为什么vim为您提供空行而不是删除它们的原因。 .与新行不匹配(请参阅:help :regex):

.   (with 'nomagic': \.)                */.* */\.*
    Matches any single character, but not an end-of-line.

\n  matches an end-of-line              */\n*
    When matching in a string instead of buffer text a literal newline
    character is matched.

因此,为了删除这些行,请按以下步骤更改正则表达式:

:%s/.*linuxserver.*\n//

哦,/g修饰符在你的情况下没有意义,因为你已经匹配整行(.* ... .*)。