Using sed to update a property in a java properties file

I need a simple one liner with sed to update the value of the java property. Not knowing what the current setting of the java property is, and it may be empty)

before

 example.java.property=previoussetting 

after

 example.java.property=desiredsetting 
+8
java sed
source share
2 answers

Assuming Linux Gnu sed, 1 solution would be

Editing is shielded '.' chars ie s/example\.java.../ for the correct comment Kent

  replaceString=desiredsetting sed -i "s/\(example\.java\.property=\).*\$/\1${replaceString}/" java.properties 

If your sed does not comply with -i , you need to manage the tmp files, i.e.

  sed "s/\(example\.java\.property=\).*\$/\1${replaceString}/" java.properties > myTmp /bin/mv -f myTmp java.properties 

Hope this helps.

+7
source share

This will update your file:

 sed -i "/property.name=/ s/=.*/=newValue/" yourFile.properties 

This will print to a new file.

 sed "/property.name=/ s/=.*/=newValue/" yourFile.properties > newFile.properties 

This is how you update several properties

 sed -i -e "/property.name.1=/ s/=.*/=newValue1/" -e "/property.name.2=/ s/=.*/=newValue2/" yourFile.properties 

Guru sed can blame me, because this is not the right way to do this (for example, I did not avoid the dots), but I consider this the best option if you do not want to sacrifice readability.

Here's an extended discussion: How do I use sed to modify my configuration files with flexible keys and values?

+9
source share

All Articles