Yalnızca belirli satırlara BASH olarak bir satır ekleme

0

Soru

BASH kullanarak Nagios eklenti hizmet tanımında değişiklik yapmak için bir komut dosyası üzerinde çalışıyorum. Kişi grubu adı satırını eklemem gerekiyor, ancak yalnızca belirli hizmet tanımları için. Bununla başlayayım diye.

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
        }

Ve sadece seçili kişi grubu satırlarını eklemek istiyorum. Bu yüzden, bunun gibi Linux hizmetlerine ek bir iletişim grubu eklemek istediğimi varsayalım.

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
        }

Bunu sed veya awk veya başka bir şey kullanarak yapabilmemin bir yolu var mı?

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

En iyi cevabı

2

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

Dosyayı güncellemek için:

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

Kullanım sed, eğer dize linux-service benzersizdir, dizeyi içeren satırdan dizeyi içeren satıra eşleştirmeyi deneyebilirsiniz contact_groups ek grubu maçın içine ekleme.

$ 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

O amele mükemmel. Değiştirmem gereken tek şey sed'den sonra add-ı idi, bu yüzden devam etti. Yanıtınız için teşekkür ederim.
Jim Miller

@ JimMiller Rica ederim. Yerinde şartın farkında değildim, özür dilerim.
HatLess

Endişeye gerek yok. En zor kısmı atlatmamı sağladın. İyi günler.
Jim Miller

Diğer dillerde

Bu sayfa diğer dillerde

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