Sed 我想在匹配多个模式时检索分隔符之间的文本

Sed 我想在匹配多个模式时检索分隔符之间的文本,sed,Sed,我有这样一个文件: define service { host_name MyHOST service_description Uptime obsess_over_service 1 low_flap_threshold 0.000000 high_flap_threshold 0.000000 stalking_options n

我有这样一个文件:

define service {
        host_name       MyHOST
        service_description     Uptime
        obsess_over_service     1
        low_flap_threshold      0.000000
        high_flap_threshold     0.000000
        stalking_options        n
        freshness_threshold     5
        }
define service {
        host_name       MyHOST
        service_description     Users - Memory usage
        event_handler   notify-service-by-email
        obsess_over_service     1
        low_flap_threshold      0.000000
        high_flap_threshold     0.000000
        stalking_options        n
        freshness_threshold     5
        notes_url       rrd2graph.cgi?hostname=$HOSTNAME$&service=$SERVICEDESC$
        }
define service {
        host_name       MyHOST
        service_description     Users - Nr of Processes
        event_handler   notify-service-by-email
        obsess_over_service     1
        low_flap_threshold      0.000000
        high_flap_threshold     0.000000
        stalking_options        n
        freshness_threshold     5
        notes_url       rrd2graph.cgi?hostname=$HOSTNAME$&service=$SERVICEDESC$
        }
define service {
        host_name       MyHOST
        service_description     Users - Processor usage
        event_handler   notify-service-by-email
        obsess_over_service     1
        low_flap_threshold      0.000000
        high_flap_threshold     0.000000
        stalking_options        n
        freshness_threshold     5
        notes_url       rrd2graph.cgi?hostname=$HOSTNAME$&service=$SERVICEDESC$
        }
我想检索{和}之间的文本,其中我匹配主机名和服务描述

我已经尝试了,但它只返回主机名、服务描述和新鲜度阈值:

sed -n -e "/define service[[:space:]]*{/,/}/ {
                /host_name/ h
                /service_description/ H
                /freshness_threshold/G
                s/freshness_threshold.*$HOST.*service_description.*$SERVICE.*$/\0/p
        }" objects.cache
使用
awk

awk -v RS="define service {|}" '/host_name *HOST/ && /service_description *SERVICE/' File
Eample:

awk -v RS="define service {|}" '/host_name *MyHOST/ && /service_description *Users - Memory usage/' File

        host_name       MyHOST
        service_description     Users - Memory usage
        event_handler   notify-service-by-email
        obsess_over_service     1
        low_flap_threshold      0.000000
        high_flap_threshold     0.000000
        stalking_options        n
        freshness_threshold     5
        notes_url       rrd2graph.cgi?hostname=$HOSTNAME$&service=$SERVICEDESC$

sed'/define服务{/{:1;/}/!{N;b1};/'“$Yourvar”'/p};d'
Hi,我需要上面的不仅匹配一个变量,而且同时匹配两个变量。然后再放置另一个变量。你是对的。非常感谢。sed'/define服务{/{:1;/}/!{N;b1};/“$Yourvar”。*“$Anothervar”'/p};d'Hi,与awk配合很好。非常感谢。我只需要了解如何在中使用bash变量,以及如何使awk在搜索类似“text[info]text”的内容时不使用正则表达式。手动转义“文本[信息]文本”时工作。