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
I have a file called "config.php" and I wanted to change one of its definitions lines.
For example, the line:
had to be replaced with this one:
So I did that:
In the first part, I am looking for a line that contains "SOME_CONSTANT" (thus the wildcards)
Then I replace that line with a new definition such as: define('SOME_CONSTANT', 'new_value');
Tested and works fine in Centos 7
I used this script for keeping the priorities..
The arguments $1 will have folder in which multiple config files exist. $2 will have properties which need to be replaced in $1 path and sub paths files #3 will have properties which need to be override on top of $2
It also has hidden logic to check for existence of environment variables for the keys exist in $2 and $3 and give priority to that.
i.e If a key exist in environment that would be highest priority. Next to that would $3 and next to that would $1 file.
I know it is too late to add an answer to this question however, I thought to share my knowledge to you all. There is a very general approach which I have followed to solve a similar kind of problem. I have deleted the whole line which is matching the string and added the required values to that key. To your question here is the answer
sed deletes the matching string line from the file and the immediate next line is inserting the required key and value to the file.
I like using
awk
for this, since it is quite easy to understand what it is doing and takes care very well of the separator (=
) and also the fact that it must be done to an uncommented line:This uses
match()
to check if the pattern occurs in any given line. If it does, it performs the replacement with the given value.For example:
Let's change the value in
my_var
toNEW VALUE
:It is also possible to set the values in shell variables and then use them with
-v
:And you can of course put all of this within a shell function that you then call normally:
Upon execution, this returns
While you can also make the script receive the parameters in a way like:
./script.sh "conf_file" "var_to_replace" "NEW VALUE"
to then pass them to the function.Here's an example expression:
If you want to match stuff with
/
in it, you can use another delimiter:Or with variable expansion:
In your example:
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:
And ultimately piping to ssh (not tested):
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.
If you want to replace between 2 property files you can use this: