943,693 Members | Top Members by Rank

Ad:
Sep 23rd, 2009
-1

find a paragraph in a file then delete it

Expand Post »
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. }
Similar Threads
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Sep 23rd, 2009
0

Re: find a paragraph in a file then delete it

Invert the match:
Shell Scripting Syntax (Toggle Plain Text)
  1. awk '{ FS="\n" ; RS="define" } $RS !~ /check_chris/ { print }' file
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 24th, 2009
0

Re: find a paragraph in a file then delete it

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.
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Sep 24th, 2009
0

Re: find a paragraph in a file then delete it

Just stick it back in:
Shell Scripting Syntax (Toggle Plain Text)
  1. awk '{ FS="\n" ; RS="define" } $RS !~ /check_chris/ { print "define" $RS }' file
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 24th, 2009
0

Re: find a paragraph in a file then delete it

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. }
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Sep 24th, 2009
0

Re: find a paragraph in a file then delete it

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
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 24th, 2009
0

Re: find a paragraph in a file then delete it

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!
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: directory renaming ... related
Next Thread in Shell Scripting Forum Timeline: cat . causes strange characters





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC