-1

我想打印{}中的行,并在 Hostgroups 中分配 where "mango"

   object Host "os.google.com" {
    import "windows"
    address = "linux.google.com"
    groups = ["linux"]
    }

    object Host "mango.google.com" {
    import "windows"
    address = "mango.google.com"
    groups = ["linux"]

    assign where "mango" in Hostgroups
    }

期望的输出:

    object Host "mango.google.com" {
    import "windows"
    address = "mango.google.com"
    groups = ["linux"]

    assign where "mango" in Hostgroups
    }
4

3 回答 3

0

假设}在您的输入中没有出现在任何其他上下文中:

$ awk -v RS='}' '
    /assign where "mango" in Hostgroups/ {
        sub(/^[[:space:]]+\n/,"")
        print $0 RS
    }
' file
    object Host "mango.google.com" {
    import "windows"
    address = "mango.google.com"
    groups = ["linux"]

    assign where "mango" in Hostgroups
    }
于 2019-06-23T21:57:47.750 回答
0

这可能对您有用(GNU sed):

sed -n '/{/h;//!H;/}/{g;/assign where "mango" in Hostgroups/p}' file

使用该选项关闭 seds 自动打印,-n并在花括号之间的保持空间中收集线条。在右花括号之后,将其替换为保留空间的内容,如果匹配则assign where "mango" in Hostgroup打印它。

于 2019-06-22T22:07:11.097 回答
0

试试这个awk脚本

script.awk

/{/,/}/ { #define record range from { to }
    if ($0 ~ "{") rec = $0; # if record opening reset rec variable with current line
    else rec = rec "\n" $0; # else accumulate the current line in rec
    if ($0 ~ /assign where "mango" in Hostgroups/) { # if found exit pattern in current line
        print rec; # print the rec
        exit;      # terminate
    }
}

处决:

awk -f script.awk input.txt

输出:

object Host "mango.google.com" {
import "windows"
address = "mango.google.com"
groups = ["linux"]

assign where "mango" in Hostgroups
于 2019-06-22T20:12:35.777 回答