추가 줄에서만 특정 노선

0

질문

나는 스크립트를 변경하는 컴파일러는 필요하지 않은 플러그인이 서비스의 정의를 사용하여 BASH. 필요에 추가 연락처 그룹의 이름을 줄만을 위해 특정 서비스를 정의 합니다. 그래서 내가 시작이다.

define service {
    use                     sites-service
    host_name               my_host
    service_description     check_reboot_os_updates
    check_command           check_reboot_os_updates
    contact_groups          contactgroup1
    servicegroups           MyGroup
    }
    
define service {
        use                     linux-service
        host_name               my_host
        service_description     other_description
        check_command           other_command
        contact_groups          contactgroup1
        servicegroups           MyGroup
        }

하고 싶만 추가 연락처 선택 그룹 라인입니다. 그렇게 말하고 싶었을 추가 연락처 그룹의 리눅스 서비스는 다음과 같다.

define service {
        use                     sites-service
        host_name               my_host
        service_description     check_reboot_os_updates
        check_command           check_reboot_os_updates
        contact_groups          contactgroup1
        servicegroups           MyGroup
        }
        
define service {
        use                     linux-service
        host_name               my_host
        service_description     other_description
        check_command           other_command
        contact_groups          contactgroup1, contactgroup2
        servicegroups           MyGroup
        }

은 거기에는 방법이 사용하여 이 작업을 수행할 수 있습 sed 또는 awk 또는 다른 뭔가?

awk bash nagios sed
2021-11-23 20:51:30
2

최고의 응답

2

으로

awk '
  $1 == "use" {use = $2}
  use == "linux-service" && $1 == "contact_groups" {$0 = $0 ", contactgroup2"}
  {print}
' file

파일을 업데이트:

  • gawk -i inplace '...' file
  • awk '...' file | sponge file -필요 moreutils 패키지
  • f=$(mktemp); awk '...' file > "$f" && mv "$f" file
2021-11-23 21:28:53
0

sed는 경우에,문자열 linux-service 은 독특한 시도할 수 있습니다 일치하는 라인에서 문자열이 포함된 라인을 포함하는 문자열 contact_groups 추가하여 추가적인 그룹 내에서 일치합니다.

$ sed '/linux-service/,/contact_groups/s/contact_groups.*/&, contactgroup2/' input_file
define service {
        use                     sites-service
        host_name               my_host
        service_description     check_reboot_os_updates
        check_command           check_reboot_os_updates
        contact_groups          contactgroup1
        servicegroups           MyGroup
        }

define service {
        use                     linux-service
        host_name               my_host
        service_description     other_description
        check_command           other_command
        contact_groups          contactgroup1, contactgroup2
        servicegroups           MyGroup
2021-11-23 21:19:20

이것은 완벽하게 작동되었습니다. 일반을 변경했었는 추가 난 후 sed 그래서 그것은 유지됩니다. 귀하의 답변 주셔서 감사합니다.
Jim Miller

@JimMiller 당신을 환영합니다. 나는 알고의 장소에서 요구 사항,사과이다.
HatLess

아무 걱정하지 않습니다. 당신은 나에게 가장 어려운 부분입니다. 좋은 날입니다.
Jim Miller

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................