linux - How do I use sed to change my configuration files, with flexible keys and values? -


i want search configuration file expression: "central.database". want change setting associated "central.database" "sqltest".

the layout of config file initially:

central.database = sqlfirsttest 

this want after sed replacement:

central.database = sqltest 

i doing in bash script, suggestions, recommendations or alternative solutions welcome!

(actually both central.database , sqltest come bash variables here.)


my current code (third attempt):

sshretvalue=$(ssh -p "35903" -i $home/sshids/idrsa-1.old ${1} <<eof         sed -i "s/^\($central_db_name\s*=\s*\).*\$/\1$central_db_value/" /home/testing.txt;         echo $? eof ) 

error message:

pseudo-terminal not allocated because stdin not terminal. sed: -e expression #1, char 58: unknown option `s' -bash: line 3: eof: command not found 

here's example expression:

sed -i 's/^\(central\.database\s*=\s*\).*$/\1sqltest/' file.cfg 

if want match stuff / in it, can use delimiter:

sed -i 's#^\(cent/ral\.data/base\s*=\s*\).*$#\1sql/test#' file.cfg 

or variable expansion:

val="sqltest" sed -i "s/^\(central\.database\s*=\s*\).*\$/\1$val/" file.cfg 

in example:

sshretvalue=`sed -i "s/^\(\1$central_db_name\s*=\s*\).*\$/\1$central_db_value/" /home/testing.txt`; 

there's \1 before $central_db_name that's invalid. also, sed doesn't print it's return value. preferred way check return values:

sed -i "s/^\($central_db_name\s*=\s*\).*\$/\1$central_db_value/" /home/testing.txt; sed_return_value=$? 

and piping ssh (not tested):

sed_return_value=$(ssh server <<eof     sed -i "s/^\($central_db_name\s*=\s*\).*\$/\1$central_db_value/" /home/testing.txt;     echo $? eof ) 

the -i replacing data in input file. otherwise sed writes stdout.

regular expressions field of own. impossible explain them in depth in stackoverflow answer, unless there specific function that's eluding you.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -