Search Replace and Update a file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Search Replace and Update a file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-04-2010
raghu_shekar raghu_shekar is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 28 November 2011, 2:32 AM EST
Posts: 57
Thanks: 9
Thanked 1 Time in 1 Post
Search Replace and Update a file

hi,
I am stuck at a place. Please help me out. Here is what i need to do.
Search for a pattern in a propertyfile and change only at one occurance. I have these statements and assignment as a part of the propertyfile

Code:
 #Note : The Address should be replaced with actual address dynamically.
 ServerName=Address

Now i want to search the propertyfile, and change Address only in the assignment and not in the comment. Somethinglike

Code:
#Note : The Address should be replaced with actual address dynamically.
 ServerName=Actual Address

Here is what is my thought:

Code:
x=`grep ServerName Property.file |awk -F= '{print $2}'`
sed -i "s/$x/Actual Address/" Property.file

But the above code will replace "Address" with "Actual Address" even in the comment part.
how can i achieve this... Is there a way i can use sed inside awk, or pipe the result of awk to sed and change the property file
Any help is greately appriciated
Sponsored Links
    #2  
Old 11-04-2010
pravin27 pravin27 is offline Forum Advisor  
Advisor
 
Join Date: Sep 2009
Last Activity: 21 May 2014, 8:12 AM EDT
Location: ./India/Mumbai
Posts: 1,208
Thanks: 55
Thanked 269 Times in 262 Posts
Try this,

Code:
 
perl -nle 's/(?<=ServerName\=)Address/Actual Address/; print $_;' inputfile

Sponsored Links
    #3  
Old 11-04-2010
michaelrozar17's Avatar
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 17 March 2014, 1:43 AM EDT
Location: IN
Posts: 892
Thanks: 14
Thanked 182 Times in 173 Posts
by sed..

Code:
sed 's/\(ServerName=\).*/\1Actual Address/g' inputfile > outfile

    #4  
Old 11-04-2010
walid2mi's Avatar
walid2mi walid2mi is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 10 November 2010, 6:31 AM EST
Location: Tunisia,Mornaguia
Posts: 15
Thanks: 1
Thanked 2 Times in 1 Post
untested:

Code:
sed "s/=.*/=Actual Address/" file

Sponsored Links
    #5  
Old 11-04-2010
raghu_shekar raghu_shekar is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 28 November 2011, 2:32 AM EST
Posts: 57
Thanks: 9
Thanked 1 Time in 1 Post
hi walid2mi,
As per your code, it will change all the occurances of any value after "=" with the new string..

Michaelrozar17,
I tested your code and it replaces the entire string with the new string.
before your code

Code:
ServerName=Address

After your code: i.e

Code:
sed 's/\(ServerName=\).*/Actual Address/g' Property.file

we get

Code:
Actual Address

instead of

Code:
ServerName=Actual Address

---------- Post updated at 03:22 PM ---------- Previous update was at 03:15 PM ----------

Hi michaelrozar17
Your code works fine, i had missed out the "1".
Can you please explain the code and how it works...

Thanks!!!
Sponsored Links
    #6  
Old 11-04-2010
michaelrozar17's Avatar
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 17 March 2014, 1:43 AM EDT
Location: IN
Posts: 892
Thanks: 14
Thanked 182 Times in 173 Posts
Quote:
Originally Posted by raghu_shekar View Post
Michaelrozar17,
I tested your code and it replaces the entire string with the new string.
before your code

Code:
ServerName=Address

After your code: i.e
[CODE]sed 's/\(ServerName=\).*/Actual Address/g' Property.file
hmm.. looks like you have missed the save part (\1) in the sed command. Pls take a look at the command again posted earlier

Code:
sed 's/\(ServerName=\).*/\1Actual Address/g' inputfile > outfile

Sponsored Links
    #7  
Old 11-04-2010
raghu_shekar raghu_shekar is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 28 November 2011, 2:32 AM EST
Posts: 57
Thanks: 9
Thanked 1 Time in 1 Post
Hi michaelrozar17
I realised it and fixed it. but did not understand it...

Can you please explain what the code does and how. Actually i did not understand teh "\1".
Otherwise i think, the sed command looks for any string that appears after
"ServerName=" and replaces with the new string. but why \1 in front of the new string. I am not understanding that. I am not very familiar with sed
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search and replace in xml file using awk.. islam2666 Shell Programming and Scripting 4 02-28-2010 09:08 PM
search and replace in a file ammu UNIX for Advanced & Expert Users 3 06-17-2009 05:20 AM
search and replace in a file satyam_sat Shell Programming and Scripting 2 04-11-2008 01:28 AM
Search and replace in file.. Amits Shell Programming and Scripting 2 08-22-2006 07:11 PM
Search and replace in file malaymaru UNIX for Dummies Questions & Answers 3 05-03-2005 06:57 AM



All times are GMT -4. The time now is 08:56 PM.