| | |
find a paragraph in a file then delete it
Thread Solved |
•
•
Join Date: Feb 2006
Posts: 399
Reputation:
Solved Threads: 14
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.
File
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.
Shell Scripting Syntax (Toggle Plain Text)
awk '{ FS="\n" ; RS="define" } /STRING/ { print }' FILENAME e.g. awk '{ FS="\n" ; RS="define" } /check_chris/ { print }' xeno.cfg
Shell Scripting Syntax (Toggle Plain Text)
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 }
If my post helped add to my rep!
Invert the match:
Shell Scripting Syntax (Toggle Plain Text)
awk '{ FS="\n" ; RS="define" } $RS !~ /check_chris/ { print }' file
•
•
Join Date: Feb 2006
Posts: 399
Reputation:
Solved Threads: 14
hi,
That also removes the word define from every other serivce e.g:
before
After
Anyway to stop this happening.
That also removes the word define from every other serivce e.g:
before
Shell Scripting Syntax (Toggle Plain Text)
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 }
Shell Scripting Syntax (Toggle Plain Text)
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
If my post helped add to my rep!
Just stick it back in:
Shell Scripting Syntax (Toggle Plain Text)
awk '{ FS="\n" ; RS="define" } $RS !~ /check_chris/ { print "define" $RS }' file
•
•
Join Date: Feb 2006
Posts: 399
Reputation:
Solved Threads: 14
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?:
Shell Scripting Syntax (Toggle Plain Text)
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 }
If my post helped add to my rep!
I do a bad job at checking my work it seems. There are two problems:
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:
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:
Shell Scripting Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- c# nse File.Delete cannot delete "Temporary Internet Files" files,help me (C#)
- If I want to insert a paragraph from a file to LinkList, how? (C++)
- plz help me in solving this files concept question (Java)
- can not find script file "C:\achi.dll.vbs" (Viruses, Spyware and other Nasties)
- Sending 'Find' output to a file? (*nix Software)
- Find word in file!! (C++)
- Need help in reviewing HJT file (Viruses, Spyware and other Nasties)
- about:blank hijack (can't find any .dll-file) (Viruses, Spyware and other Nasties)
Other Threads in the Shell Scripting Forum
- Previous Thread: directory renaming ... related
- Next Thread: cat . causes strange characters
| Thread Tools | Search this Thread |






