script to grep a pattern from file compare contents with another file and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to grep a pattern from file compare contents with another file and replace
# 1  
Old 08-30-2011
script to grep a pattern from file compare contents with another file and replace

Hi All,
Need help on this

I have 2 files
one file file1 which has several entries as :
Code:
define service{
hostgroup_name    !host1,!host5,!host6,.*
service_description check_nrpe
}
 
define service{
hostgroup_name    !host2,!host4,!host6,.*
service_description check_opt
}

another file file2:
Code:
host1
host2
host3
host4
host5
host6

I wanted to compare the entries in the line
"hostgroup_name !host1,!host5,!host6,.*" of file1 for each service definition with the contents of the file2 and replace the entries under the hostgroup_name with the missing contents removing the existing content

i.e.,

i wanted the ouput as
Code:
define service{
hostgroup_name    host2,host3,host4
service_description check_nrpe
}
 
define service{
hostgroup_name    host1,host3,host5
service_description check_opt
}

Smilie
Thanks,
namitai
# 2  
Old 08-30-2011
Assuming a single line hostgroup_names and no embedded white space characters in the hostnames.

Code:
awk 'NR == FNR {
  h[$0]; next
  }
/hostgroup_name/ {
  split($2, t, /[,!]/)
  split(x, _h); hs = x
  for (T in t) _h[t[T]]
  for (H in h) H in _h || hs = hs ? hs "," H : H
  sub(/[^ ]*$/, hs) 	
  }1' file2 file1

This User Gave Thanks to radoulov For This Post:
# 3  
Old 08-30-2011
Thanks Indeed It works perfectly fine......

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to replace a string with pattern read from a file

I have two files blocks.txt and rules.txt. In blocks.txt i have the following entries Linux1 Linux2 Linux3 ..... Linux10 In rules.txt i have the lines where a filename pattern starts like 'blk-name.*' I want to replace 'blk-name' with the names read from blocks.txt file I tried... (2 Replies)
Discussion started by: Jag02
2 Replies

2. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

3. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

4. Shell Programming and Scripting

Grep a pattern given in one file at other file and display its corresponding contents as output.

***************************************** Right now i have this current system. I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which... (7 Replies)
Discussion started by: abinash
7 Replies

5. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

6. Shell Programming and Scripting

shell script to compare file contents

Hello Has anyone got an example shell script that I can use to compare the contents of two files. The files should contain the same contents, eg. file1.txt apple pear grape file2.txt apple pear grape (2 Replies)
Discussion started by: deedaz
2 Replies

7. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

8. Shell Programming and Scripting

grep a pattern and replace a value in it and write to the same file.

I have some complication with this, I have a file like below for DEV_1 till DEV_10. and the db values are set accordinly which are not unique. For example DEV1,DEV4,DEV6 can have the same target DB name. I waned to identify for DEV_2 and then replace the TARGET_DATABASE value with the new DB... (6 Replies)
Discussion started by: yesmani
6 Replies

9. Shell Programming and Scripting

Script to check a file and replace some of the contents

Hi I have a file that looks like this: Line 0 animal elephant Line 1 animal elephant Line 2 animal elephant Line 3 animal elephant What i am aiming to do is with a script and an input value of... (6 Replies)
Discussion started by: tara
6 Replies

10. Shell Programming and Scripting

Compare & replace contents within a file

I have 2 files file1 1 TMQUEUE QUE1 STMW633A 100 DMADM DOMGRPSTMW633A STMW633A 100 GWADM GWTGRPSTMW633A STMW633A 100 GWADM GWTGRPSTMW633AA STMW633A 100 GWADM GWTGRPSTMW638A STMW638A 100 TMSYSEVT EVTGRPSTMW633A STMW633A 100 TMSYSEVT ... (2 Replies)
Discussion started by: kaustubh137
2 Replies
Login or Register to Ask a Question