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
#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
#Note : The Address should be replaced with actual address dynamically.
ServerName=Actual Address
Here is what is my thought:
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
Try this,
perl -nle 's/(?<=ServerName\=)Address/Actual Address/; print $_;' inputfile
by sed..
sed 's/\(ServerName=\).*/\1Actual Address/g' inputfile > outfile
untested:
sed "s/=.*/=Actual Address/" file
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
ServerName=Address
After your code: i.e
sed 's/\(ServerName=\).*/Actual Address/g' Property.file
we get
Actual Address
instead of
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!!!
[quote=raghu_shekar;302468892]
Michaelrozar17,
I tested your code and it replaces the entire string with the new string.
before your code
ServerName=Address
After your code: i.e
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
sed 's/\(ServerName=\).*/\1Actual Address/g' inputfile > outfile
Hi michaelrozar17
I realised it and fixed it. but did not understand it... :o
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
Sure..actually the word ServerName= is saved by placing it inside the parenthesis i.e \(ServerName=\) and printing it again by calling \1. In general \( \) is used to save a pattern/part of word and \n is used to recall the saved pattern/part.. and \n could be from \1 to \9 i.e you could save upto 9 patterns/parts
ex:
echo "adbc1234 try" | sed 's/\(.*\) \(.*\)/\2/'
output: try
echo "adbc1234 try" | sed 's/\(.*\) \(.*\)/\1/'
output: adbc1234
echo "adbc1234 try" | sed 's/\(.*\) \(.*\)/\2 \1/'
output: try adbc1234
1 Like