Hi Guys,

I have a file with lots of similar records example shown at end, I want to be able to delete one of the records and have the following line of code which finds the record I need to delete, my question is now I have found the record how do I delete it and keep the rest of the file intact.

awk '{ FS="\n" ; RS="define" } /STRING/ { print }' FILENAME
e.g.
awk '{ FS="\n" ; RS="define" } /check_chris/ { print }' xeno.cfg

File

define service{
notification_interval           30
check_period                    24x7
notification_options c
contact_groups                  unix
use generic-service
host_name xeno
service_description Check dmesg for fails
check_command check_nrpe!check_dmesg
}
define service{
notification_interval           30
check_period                    24x7
notification_options c
contact_groups                  unix
use generic-service
host_name xeno
service_description Check Prtdiag
check_command check_nrpe!check_prtdiag
}
define service{
notification_interval           30
check_period                    24x7
notification_options c
contact_groups                  unix
use generic-service
host_name xeno
service_description Client Version
check_command check_nrpe!check_uname
}
define service{
notification_interval           30
check_period                    24x7
notification_options c
notification_options c
contact_groups                  unix
use generic-service
host_name xeno
service_description DNS
check_command check_nrpe!check_dns
}
define service{
notification_interval           30
check_period                    24x7
notification_options c
contact_groups                  unix
use generic-service
host_name xeno
service_description Check NTP
check_command check_nrpe!check_ntp
}
define service{
notification_interval           30
check_period                    24x7
use generic-service
host_name xeno
service_description Check Memory
check_command check_nrpe!check_mem
notification_options n
contact_groups unix
}
define service{
notification_interval           30
check_period                    24x7
use generic-service
host_name xeno
service_description Check Swap
check_command check_nrpe!check_swap
notification_options n
contact_groups unix
}
define service{
notification_interval           30
check_period                    24x7
use generic-service
host_name xeno
service_description Load
check_command check_nrpe!check_load
notification_options n
contact_groups unix
}
define service{
notification_interval           30
check_period                    24x7
use generic-service
host_name xeno
service_description CPU
check_command check_nrpe!check_cpu
notification_options n
contact_groups unix
}
define service{
notification_interval           30
check_period                    24x7
use generic-service
host_name xeno
service_description Check IO Wait
check_command check_nrpe!check_vmio
notification_options n
contact_groups unix
}
define service{
notification_interval           30
check_period                    24x7
use generic-service
host_name xeno
service_description Check IO Wait
check_command check_nrpe!check_chris
notification_options n
contact_groups unix
}

Recommended Answers

All 6 Replies

Invert the match:

awk '{ FS="\n" ; RS="define" } $RS !~ /check_chris/ { print }' file

hi,

That also removes the word define from every other serivce e.g:

before

define service{
notification_interval           30
check_period                    24x7
notification_options c
contact_groups                  unix
        use generic-service
        host_name xeno
        service_description Current Users
        check_command check_nrpe!check_users
}

After

service{
notification_interval           30
check_period                    24x7
notification_options c
contact_groups                  unix
        use generic-service
        host_name xeno
        service_description Check Root Volume
        check_command check_nrpe!check_root
}

 service{
notification_interval           30
check_period                    24x7
notification_options c
contact_groups                  unix
        use generic-service
        host_name xeno
        service_description Check Var Volume
        check_command check_nrpe!check_var
}
etc etc

Anyway to stop this happening.

Just stick it back in:

awk '{ FS="\n" ; RS="define" } $RS !~ /check_chris/ { print "define" $RS }' file

Cool one last thing! It didnt remove the record seperator for the first record so when I run the code you gave I get definedefine for the first record example below. Any help?:

definedefine service{
definenotification_interval           30
check_period                    24x7
notification_options c
contact_groups                  unix
        use generic-service
        host_name xeno
        service_description Current Users
        check_command check_nrpe!check_users
}


define service{
notification_interval           30
check_period                    24x7
notification_options c
contact_groups                  unix
        use generic-service
        host_name xeno
        service_description Check Root Volume
        check_command check_nrpe!check_root
}


define service{
notification_interval           30
check_period                    24x7
notification_options c
contact_groups                  unix
        use generic-service
        host_name xeno
        service_description Check Var Volume
        check_command check_nrpe!check_var
}

I do a bad job at checking my work it seems. There are two problems:

definedefine service{
definenotification_interval           30

Awk is handling the first record differently and for this I don't have an answer. Its sticking the define down a row. I'm sure this is not the "proper" way but at this point I would do:

awk '{ RS="define" } $RS !~ /check_chris/ { print "define" $RS }' file | sed 's/definedefine service/define service/g' | sed 's/definenotification_interval/notification_interval/g' | less

nice bit of a hack but seems to work ta very much! only fault is it adds two new lines after every service but I will strip those out with sed!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.