gpt4 book ai didi

linux - 在文件中的匹配字符串(模式 1)上方打印特定单词直到匹配字符串(模式 2)

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:54 33 4
gpt4 key购买 nike

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
}

我想打印匹配字符串上方的行,直到文件中的特定模式

例如:

我想打印 assign where "mango"in Hostgroups 上面的行,直到文件中的这个模式 {

期望的输出:

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

assign where "mango" in Hostgroups

最佳答案

试试这个 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

关于linux - 在文件中的匹配字符串(模式 1)上方打印特定单词直到匹配字符串(模式 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56715510/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com