shellsedkylin

sed command to find and replace string with special characters to a string in command line


I am trying to use sed to find/replace a string with special characters in a .property file.

This is my original line in the file:

kylin.source.hive.beeline-params=-n root --hiveconf hive.security.authorization.sqlstd.confwhitelist.append='mapreduce.job.*|dfs.*' -u jdbc:hive2://localhost:xxxx

I need to replace :

root --hiveconf hive.security.authorization.sqlstd.confwhitelist.append='mapreduce.job.*|dfs.*'

with

hadoop

and other string :

localhost

with

ip-00-00-00-000.ec2.internal

Final output needs to look like :

kylin.source.hive.beeline-params=-n hadoop -u jdbc:hive2://ip-00-00-00-000.ec2.internal:xxxx

I have tried a few different formats using sed:

sudo sed -i 's/root --hiveconf hive\.security\.authorization\.sqlstd\.confwhitelist\.append=\'mapreduce\.job\.\*\|dfs\.\*\'/hadoop' /usr/local/kylin/kylin.properties
sudo sed -i 's/root \-\-hiveconf hive\.security\.authorization\.sqlstd\.confwhitelist\.append\=\'mapreduce\.job\.\*\|dfs\.\*\'/hadoop' /usr/local/kylin/kylin.properties
sudo sed -r 's/root \-\-hiveconf hive\.security\.authorization\.sqlstd\.confwhitelist\.append\=\'mapreduce\.job\.\*\|dfs\.\*\'/hadoop' /usr/local/kylin/kylin.properties

I am not getting any output when I execute the above commands,its is waiting for another input.Can someone help me with this problem?


Solution

  • Here:

    sed -i "s#root.*dfs.*'#hadoop#g; s#localhost#ip-00-00-00-000.ec2.internal#g" /usr/local/kylin/kylin.properties
    

    will output:

    kylin.source.hive.beeline-params=-n hadoop -u jdbc:hive2://ip-00-00-00-000.ec2.internal:xxxx
    

    or to replace only first occurrence in file:

    sed -i "0,/root.*dfs/{s#root.*dfs.*'#hadoop#g; s#localhost#ip-00-00-00-000.ec2.internal#g}" /usr/local/kylin/kylin.properties