ต่อไปยังท้ายบรรทัดในตบเท่านั้นแน่นอนบรรทัด

0

คำถาม

ฉันทำงานสคริปต์จะทำการเปลี่ยนแปลงไปเป็น Nagios โปรแกรมเสริมการบริการนิยามโดยใช้ตบ. ฉันต้องการที่จะต่อไปยังท้ายติดต่อชื่อกลุ่มเส้นแต่เพียงสำหรับบางบริการความหมายของคำ. ดังนั้นฉันจะเริ่มต้นกับเรื่องนี้

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 หรือภาษาโปรแกรม awklanguage section หรือบางอย่างอื่นอีกมั้ย?

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 appending คนเพิ่มเติมกลุ่มภายในที่ตรงกัน

$ 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 ดังนั้นมัน persisted. ขอบคุณสำหรับการตอบสนองเลย
Jim Miller

@JimMiller ยินดีต้อนรับ ฉันไม่รู้เรื่องของอยู่ในสถานที่สำคัญที่สุขออภัย.
HatLess

ไม่ต้องห่วง คุณทำให้ฉันตรสิ่งที่ยากที่สุกยากต่อ. ขอให้สนุกแล้วกัน
Jim Miller

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

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

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่