我要在 {和} 中打印行,并分配主机组中的“芒果”
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
}
答案 0 :(得分: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
答案 1 :(得分:0)
这可能对您有用(GNU sed):
sed -n '/{/h;//!H;/}/{g;/assign where "mango" in Hostgroups/p}' file
使用-n
选项关闭seds自动打印,并在花括号之间的保留空间中收集线条。在右花括号后,将其替换为容纳空间的内容,如果与assign where "mango" in Hostgroup
匹配,则打印它。
答案 2 :(得分: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
}