Issue
I want to search a configuration file for this expression: "central.database". I then want to change the setting associated with "central.database" to "SQLTEST".
The layout of the config file would look like this initially:
central.database = SQLFIRSTTEST
This is what i want it to look like after the sed replacement:
central.database = SQLTEST
I am doing this in a bash script, any suggestions, recommendations or alternative solutions are welcome!
(Actually both central.database
and SQLTEST
come from 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 will not be allocated because stdin is not a terminal.
sed: -e expression #1, char 58: unknown option to `s'
-bash: line 3: EOF: command not found
Solution
Here's an example expression:
sed -i 's/^\(central\.database\s*=\s*\).*$/\1SQLTEST/' file.cfg
If you want to match stuff with /
in it, you can use another delimiter:
sed -i 's#^\(cent/ral\.data/base\s*=\s*\).*$#\1SQL/TEST#' file.cfg
Or with variable expansion:
VAL="SQLTEST"
sed -i "s/^\(central\.database\s*=\s*\).*\$/\1$VAL/" file.cfg
In your example:
sshRetValue=`sed -i "s/^\(\1$CENTRAL_DB_NAME\s*=\s*\).*\$/\1$CENTRAL_DB_VALUE/" /home/testing.txt`;
There's a \1 before $CENTRAL_DB_NAME that's invalid. Also, sed doesn't print it's return value. This is the preferred way to check return values:
sed -i "s/^\($CENTRAL_DB_NAME\s*=\s*\).*\$/\1$CENTRAL_DB_VALUE/" /home/testing.txt;
sed_return_value=$?
And ultimately piping to 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 is for replacing data in the input file. Otherwise sed writes to stdout.
Regular expressions are a field of their own. It would be impossible to explain them in depth in a stackoverflow answer, unless there is some specific function that's eluding you.
Answered By - sapht Answer Checked By - Pedro (WPSolving Volunteer)