简体   繁体   English

使用sed删除一段文本,直到第一个空白行

[英]Delete a block of text until the first blank line using sed

Before posting this question, I try a lot but regex is beyond my abilities and I fail couple of times, and I can't find what I need searching with Google or Stack Overflow. 在发布此问题之前,我做了很多尝试,但是正则表达式超出了我的能力范围,并且失败了几次,并且无法找到需要使用Google或Stack Overflow搜索的内容。

Here is my sample file: 这是我的示例文件:

Host xmpp2
    HostName 172.18.0.42
    User root
    Port 22

Host test
    HostName 172.18.201.2
    User root
    Port 2223
    IdentityFile /Users/arash/idkey.pem

Host afra
    HostName 79.175.169.10
    User root
    Port 22

Host hv1
    HostName 172.18.0.4
    User root
    Port 22

Host hv2
    HostName 172.18.0.5
    User root
    Port 22

I want to delete for example this block 我想删除例如这个块

Host test
    HostName 172.18.201.2
    User root
    Port 2223
    IdentityFile /Users/arash/idkey.pem

or any other block. 或任何其他块。 It's very simple if any of these text blocks is using a specific number of lines, but now it may be 4 lines or 5 lines or more so I need to remove from Host to first blank line, not a specific number of lines. 如果这些文本块中的任何一个正在使用特定的行数,这非常简单,但是现在它可能是4行或5行或更多行,因此我需要从Host删除到第一行空白行,而不是特定的行数。

sed '/^Host test$/,/^$/d' file

Output: 输出:

Host xmpp2
    HostName 172.18.0.42
    User root
    Port 22

Host afra
    HostName 79.175.169.10
    User root
    Port 22

Host hv1
    HostName 172.18.0.4
    User root
    Port 22

Host hv2
    HostName 172.18.0.5
    User root
    Port 22

If you want to edit your file "in place" add sed's option -i . 如果要“就地”编辑文件,请添加sed的选项-i


See: The Stack Overflow Regular Expressions FAQ 请参阅: 堆栈溢出正则表达式常见问题解答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM