Search ad replace using begin and end of the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search ad replace using begin and end of the file
# 1  
Old 12-07-2014
Search ad replace using begin and end of the file

Hello Friends ,

Please help to create script for compare and replace if not matches of set of lines .


Code:
* Primary*
Servername    Server1
Location  R201
Rack  4 
*End Primary*

*Secondary*
Server Name   Server1
IPAddress    10.24.30.10
Application  Apache
*End of Secondary*

Above is the file entry need to compare with below entry if above entry is not matches replace with below entry .

Code:
* Primary*
Servername    Server1
Location  Raleigh
Model    3650
Rack      3
*End Primary*

Thanks in advance Smilie
# 2  
Old 12-07-2014
I find your description confusing. It seems like you are saying you have two files (for example file1 and file2) and if the two files do not have the same contents, you want to replace the contents of the 1st file with the contents of the 2nd file. The simple way to do that is to forget about comparing them and just copy the contents of the 2nd file to the 1st file:
Code:
cp file2 file1

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 12-07-2014
Hello Don

My requirement is not to distrub secondary content of the file1 , I just want to replace only primary content.


Thank you
# 4  
Old 12-07-2014
This seems to work with your sample input:
Code:
ed -s file1 <<EOF
	/[*].*Primary.*[*]/,/[*].*End.*Primary.*[*]/d
	0r file2
	w
	q
EOF

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 12-08-2014
Thank you Don.. it worksSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

File write begin/end, recording time

Hello all, How can I find out the start and end of the writing file in the directory or recording time for writing file? I have a directory where small ~ 1*MB temporary files are written. After the end of the record, they are retrieved and erased. I can only find out that the files are... (4 Replies)
Discussion started by: mrAibo
4 Replies

2. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

3. Shell Programming and Scripting

Search for a Keyword in file and replace another keyword or add at the end of line

Hi I want to implement something like this: if( keyword1 exists) then check if(keyword2 exists in the same line) then replace keyword 2 with New_Keyword else Add New_Keyword at the end of line end if eg: Check for Keyword JUNGLE and add/replace... (7 Replies)
Discussion started by: dashing201
7 Replies

4. Shell Programming and Scripting

Get the sum of values in between begin and end in the file

Hi All, test file Begin Script Run at Thu Mar 14 09:24:16 PDT 2013 tst_accounts: ws zip: WS_out_20130313.tar.gz dat: test_20130313.dat count: 63574 loaded: xx pre-merge: xx post-merge: xx timestamp: Thu Mar 14 09:30:42 PDT 2013 tst_accounts: ws zip: WS_out_20130313.tar.gz dat: s_20130313.dat... (6 Replies)
Discussion started by: bmk
6 Replies

5. Programming

search a file between two begin and end strings in c

Can any one help me out with following problem... I want to search in a file which has two strings repeat each time(like start and end) i want to search between these two string in C programming. please help me with the solution. thanks in advance. (8 Replies)
Discussion started by: uday.sena.m
8 Replies

6. UNIX for Dummies Questions & Answers

Dump to another file for a begin and end Pattern

Hi All , I am newbie to linux shell scripting , below are the contents of my log file , i want the lines between a begin pattern and a end pattern for an instance , my begin Pattern is "Transaction Begins for Usr" and end pattern is "Transaction Ends for Usr" into another file Sample file... (1 Reply)
Discussion started by: SARAVANANE
1 Replies

7. Shell Programming and Scripting

Use of Begin IF ,END IF END not working in the sql script

Hi I have written a script .The script runs properly if i write sql queries .But if i use PLSQL commands of BEGIN if end if , end ,then on running the script the comamds are getting printed on the prompt . Ex :temp.sql After connecting to the databse at the sql prompt i type... (1 Reply)
Discussion started by: isha_1
1 Replies

8. Shell Programming and Scripting

begin end detection

Hi all, i am new to scripting. i need to write a code to detect begin and end of word that either begins or ends with t,th,d,dh,s,sh i have a set of words in a file containg one word per line. let the filename be aaa.txt. i have an another file bbb.txt which has two lines, just specifying the... (7 Replies)
Discussion started by: blkanth
7 Replies

9. Shell Programming and Scripting

BEGIN END questions

Why would you need to use this in a script? Why can't you just use print to print out what you want printed in the begining and print for what you want at the end. So this: nawk 'BEGIN {print "this is the first line"} {print $1 $2 $3} {print $5 $6} END {print "this is the last line"}' ... (2 Replies)
Discussion started by: llsmr777
2 Replies

10. Shell Programming and Scripting

How do you search and replace a text with markerA that end with markerB

Hi all, How do you replace all lines that begin with markerA and end with markerB? Ex: I have a file with a list of lines. /sbin/fsck -> $(SEC_CRIT) ; /sbin/fsck.ext2 -> $(SEC_CRIT) ; How can I replace all "->$....;" with a blank space... (1 Reply)
Discussion started by: drone
1 Replies
Login or Register to Ask a Question