Search code examples
bashshellsedawkproperties-file

Replace dots in the property name but not in the property value


I have a file with the following content (it's a .properties file from java):

product.company.url=http://www.example.com/
product.version=1.3.2

I need to replace all dots in the property name, but not in the property value. So, my desired output would be:

product_company_url=http://www.example.com/
product_version=1.3.2

I have tried sed -r 's/\.(\w+)([^=]*)/_\1\2/' product.default.properties but this only replaces the first dot. I think I might have to use lookaheads or lookbehinds, but I don't know if it's possible because the length of the assertion would be variable. Also there may be a variable number of dots in the property name.

Is it possible to do this with a one-liner?


Solution

  • try this line:

    awk -F= -vOFS="=" 'gsub(/\./,"_",$1)+1' file.properties
    

    test with your example:

    kent$  echo "product.company.url=http://www.example.com/
    product.version=1.3.2"|awk -F= -vOFS="=" 'gsub(/\./,"_",$1)+1'
    product_company_url=http://www.example.com/
    product_version=1.3.2