How to remove contents from file which are under bracket?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove contents from file which are under bracket?
# 1  
Old 09-07-2017
How to remove contents from file which are under bracket?

hello Friend,

In hostgroup file, i have define lots of hostgroups. I need to remove few of them without manually editing file. Need script or syntax.
I want to search particular on hostgroup_members and delete hostgoup defination of it.

for example.
Code:
define hostgroup{
        hostgroup_name          test
        alias                   test server
       hostgroup_members       test-server
}
define hostgroup{
        hostgroup_name          prd
        alias                   Prd server
       hostgroup_members       prd-server
}
define hostgroup{
        hostgroup_name          acc
        alias                   acc server
       hostgroup_members      acc-server
}

for example: suppose i delete prd-server hostgroup_members then my output will be below.
Code:
define hostgroup{
        hostgroup_name          test
        alias                   test server
       hostgroup_members       test-server
}
define hostgroup{
        hostgroup_name          acc
        alias                   acc server
       hostgroup_members      acc-server
}



Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes it easier to read and preserves multiple spaces for indenting or fixed width data.

Last edited by rbatte1; 09-07-2017 at 10:10 AM.. Reason: Added CODE tags
# 2  
Old 09-07-2017
Dear ghpradeep,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.


Kind regards,
Robin
# 3  
Old 09-07-2017
Code:
awk '{$0=$0} ; $0 !~ del && NF {print $0 RS}' RS="}" del="prd-server" hostgroup

# 4  
Old 09-08-2017
Thanks rdrtx1, above command working fine.

one query here. suppose hostgroup_members are blank. noting is there. is it possible to delete it.
example:

Code:
define hostgroup{
        hostgroup_name          acc
        alias                   acc server
       hostgroup_members     
}

# 5  
Old 09-08-2017
How about
Code:
awk '{$0=$0} ; $0 !~ del && NF {print $0 RS}' RS="}" del="hostgroup_members *\n" file

This User Gave Thanks to RudiC For This Post:
# 6  
Old 09-08-2017
More correct for a Nagios-style hostgroup.cfg is probably
Code:
awk '
  /\{/ {bl++} /\}/ {bl--}
  (bl==1 && $1=="define" && $2~/^hostgroup *\{/) {
    hostgroup=1
    hbuf=""
  }
  {
    if (hostgroup==1) {
      if ($1=="hostgroup_members") {
        nh=split($2,H,/,/)
        new2=sep=""
        for (i=1;i<=nh;i++) if (host!=H[i]) {
          new2=(new2 sep H[i]); sep=","
        }
        if (new2!=$2) { $2=new2 }
        if (new2=="") delblk=1
      }
      hbuf=(hbuf $0 ORS)
      if (bl==0) {
        if (delblk==1) {
          delblk=0
        } else {
          printf "%s", hbuf
        }
      }
    } else { print }
  }
' host=prd-server hostgroup.cfg

It won't stumble over a { } subsection, and it will delete the given host from a list
Code:
  hostgroup_members    host1,host2,host3

# 7  
Old 09-11-2017
Hello MadeInGermany,

thanks for code. I checked and its working while passing single hostgroups_members name
Can we use same for multiple hostgroups_members to remove by passing file which contain hostgroups_members name.
like putting above code inside for for.
anything need to change in code?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove bracket including text inside with sed

Hello, I could not remove brackets with text contents myfile: Please remove the bracket with text I wish to remove: I tried: sed 's/\//' myfile It gives: Please remove the bracket with text A1 I expect: Please remove the bracket with text Many thanks Boris (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Remove or rename based on contents of file

I am trying to use the two files shown below to either remove or rename contents in one of those files. If in file1.txt $5 matches $5 of file2.txt and the value in $1 of file1.txt is not "No Match" then that value is substituted for all values in $5 and $1 of file2.txt. If however in $1 ... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Remove bracket part entires and separate entries after comma

Hi all This time my input conatin 3 columns: ERCC1 (PA155) Platinum compounds (PA164713176) Allele A is not associated with response to Platinum compounds in women with Ovarian Neoplasms as compared to allele C . CES1 (PA107) methylphenidate (PA450464) Genotype CT is not... (4 Replies)
Discussion started by: Priyanka Chopra
4 Replies

4. Shell Programming and Scripting

Remove bracket part

Hi I have to remove in a file in first column whatever is written in brackets with brackets so one file hgfd 123 gfhdj 483 jdgfdg 34738 the output shuld be hgfd 123 gfhdj 483 jdgfdg 34738 (9 Replies)
Discussion started by: manigrover
9 Replies

5. Shell Programming and Scripting

ksh, difference between double bracket and single bracket

Can somebody tell me the difference between double brackets and single brackets, when doing a test. I have always been acustomed to using single brackets and have not encountered any issues to date. Why would somebody use double brackets. Ie if ] vs if Thanks to... (2 Replies)
Discussion started by: BeefStu
2 Replies

6. Shell Programming and Scripting

Remove lines based on contents of another file

So, this issue is driving me nuts! I was hoping to get a lending hand here... I have 2 files: file1.txt contains: this is example1 this is example2 this is example3 this is example4 this is example5 file2.txt contains: example3 example5 Basically, I need a script or command to... (4 Replies)
Discussion started by: bashshadow1979
4 Replies

7. Shell Programming and Scripting

Compare two files and remove all the contents of one file from another

Hi, I have two files, in which the second file has exactly the same contents of the first file with some additional records. Now, if I want to remove those matching lines from file2 and print only the extra contents which the first file does not have, I could use the below unsophisticated... (3 Replies)
Discussion started by: royalibrahim
3 Replies

8. Shell Programming and Scripting

Query:just need to remove the contents of the file without deleting that

Hii, Please suggest me how can i only remove the contents of the file without deleting it. (3 Replies)
Discussion started by: namishtiwari
3 Replies

9. Solaris

remove the contents of a file

Hi Let say a flat file contains 1000 lines. The cursor is at the 530 line number. Now I like to delete all the line at one ahot. how it can be done? (2 Replies)
Discussion started by: surjyap
2 Replies
Login or Register to Ask a Question