find a paragraph in a file then delete it

Thread Solved

Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

find a paragraph in a file then delete it

 
-1
  #1
Sep 23rd, 2009
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.
Shell Scripting Syntax (Toggle Plain Text)
  1. awk '{ FS="\n" ; RS="define" } /STRING/ { print }' FILENAME
  2. e.g.
  3. awk '{ FS="\n" ; RS="define" } /check_chris/ { print }' xeno.cfg
File
Shell Scripting Syntax (Toggle Plain Text)
  1. define service{
  2. notification_interval 30
  3. check_period 24x7
  4. notification_options c
  5. contact_groups unix
  6. use generic-service
  7. host_name xeno
  8. service_description Check dmesg for fails
  9. check_command check_nrpe!check_dmesg
  10. }
  11. define service{
  12. notification_interval 30
  13. check_period 24x7
  14. notification_options c
  15. contact_groups unix
  16. use generic-service
  17. host_name xeno
  18. service_description Check Prtdiag
  19. check_command check_nrpe!check_prtdiag
  20. }
  21. define service{
  22. notification_interval 30
  23. check_period 24x7
  24. notification_options c
  25. contact_groups unix
  26. use generic-service
  27. host_name xeno
  28. service_description Client Version
  29. check_command check_nrpe!check_uname
  30. }
  31. define service{
  32. notification_interval 30
  33. check_period 24x7
  34. notification_options c
  35. notification_options c
  36. contact_groups unix
  37. use generic-service
  38. host_name xeno
  39. service_description DNS
  40. check_command check_nrpe!check_dns
  41. }
  42. define service{
  43. notification_interval 30
  44. check_period 24x7
  45. notification_options c
  46. contact_groups unix
  47. use generic-service
  48. host_name xeno
  49. service_description Check NTP
  50. check_command check_nrpe!check_ntp
  51. }
  52. define service{
  53. notification_interval 30
  54. check_period 24x7
  55. use generic-service
  56. host_name xeno
  57. service_description Check Memory
  58. check_command check_nrpe!check_mem
  59. notification_options n
  60. contact_groups unix
  61. }
  62. define service{
  63. notification_interval 30
  64. check_period 24x7
  65. use generic-service
  66. host_name xeno
  67. service_description Check Swap
  68. check_command check_nrpe!check_swap
  69. notification_options n
  70. contact_groups unix
  71. }
  72. define service{
  73. notification_interval 30
  74. check_period 24x7
  75. use generic-service
  76. host_name xeno
  77. service_description Load
  78. check_command check_nrpe!check_load
  79. notification_options n
  80. contact_groups unix
  81. }
  82. define service{
  83. notification_interval 30
  84. check_period 24x7
  85. use generic-service
  86. host_name xeno
  87. service_description CPU
  88. check_command check_nrpe!check_cpu
  89. notification_options n
  90. contact_groups unix
  91. }
  92. define service{
  93. notification_interval 30
  94. check_period 24x7
  95. use generic-service
  96. host_name xeno
  97. service_description Check IO Wait
  98. check_command check_nrpe!check_vmio
  99. notification_options n
  100. contact_groups unix
  101. }
  102. define service{
  103. notification_interval 30
  104. check_period 24x7
  105. use generic-service
  106. host_name xeno
  107. service_description Check IO Wait
  108. check_command check_nrpe!check_chris
  109. notification_options n
  110. contact_groups unix
  111. }
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: find a paragraph in a file then delete it

 
0
  #2
Sep 23rd, 2009
Invert the match:
Shell Scripting Syntax (Toggle Plain Text)
  1. awk '{ FS="\n" ; RS="define" } $RS !~ /check_chris/ { print }' file
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: find a paragraph in a file then delete it

 
0
  #3
Sep 24th, 2009
hi,

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

before
Shell Scripting Syntax (Toggle Plain Text)
  1. define service{
  2. notification_interval 30
  3. check_period 24x7
  4. notification_options c
  5. contact_groups unix
  6. use generic-service
  7. host_name xeno
  8. service_description Current Users
  9. check_command check_nrpe!check_users
  10. }
After
Shell Scripting Syntax (Toggle Plain Text)
  1. service{
  2. notification_interval 30
  3. check_period 24x7
  4. notification_options c
  5. contact_groups unix
  6. use generic-service
  7. host_name xeno
  8. service_description Check Root Volume
  9. check_command check_nrpe!check_root
  10. }
  11.  
  12. service{
  13. notification_interval 30
  14. check_period 24x7
  15. notification_options c
  16. contact_groups unix
  17. use generic-service
  18. host_name xeno
  19. service_description Check Var Volume
  20. check_command check_nrpe!check_var
  21. }
  22. etc etc
Anyway to stop this happening.
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: find a paragraph in a file then delete it

 
0
  #4
Sep 24th, 2009
Just stick it back in:
Shell Scripting Syntax (Toggle Plain Text)
  1. awk '{ FS="\n" ; RS="define" } $RS !~ /check_chris/ { print "define" $RS }' file
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: find a paragraph in a file then delete it

 
0
  #5
Sep 24th, 2009
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)
  1. definedefine service{
  2. definenotification_interval 30
  3. check_period 24x7
  4. notification_options c
  5. contact_groups unix
  6. use generic-service
  7. host_name xeno
  8. service_description Current Users
  9. check_command check_nrpe!check_users
  10. }
  11.  
  12.  
  13. define service{
  14. notification_interval 30
  15. check_period 24x7
  16. notification_options c
  17. contact_groups unix
  18. use generic-service
  19. host_name xeno
  20. service_description Check Root Volume
  21. check_command check_nrpe!check_root
  22. }
  23.  
  24.  
  25. define service{
  26. notification_interval 30
  27. check_period 24x7
  28. notification_options c
  29. contact_groups unix
  30. use generic-service
  31. host_name xeno
  32. service_description Check Var Volume
  33. check_command check_nrpe!check_var
  34. }
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: find a paragraph in a file then delete it

 
0
  #6
Sep 24th, 2009
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:
Shell Scripting Syntax (Toggle Plain Text)
  1. 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
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: find a paragraph in a file then delete it

 
0
  #7
Sep 24th, 2009
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!
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC