How to Use Sed to Change My Configuration Files, With Flexible Keys and Values

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

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:

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;

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 $?

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.

modify a file using a bash script with sed

sed "s/\(define('_DB_NAME_', \).*/\1'new value');/" filename

and likewise for the other two.

If you want to alter the file in place, you can use -i, but you have to be careful, different versions of sed handle that differently.

Replace the lines if one part of string matches using shell script

This will change the line:

sed -iE 's/(SERVER : )([0-9.]+)/\1'"$SERVER_IP"'/' config.txt


-i                    # write changes to the file "in place".
-E # Use extended regex (to avoid the need
# of backslash in `()`)
's/ … / … /' # Use the substitute command.
(SERVER : ) # Match the string you need `SERVER : ` capturing it
# in the first group of parenthesis.
([0-9.]+) # capture digits and dots in the second group.
/\1'"$SERVER_IP"'/' # write the contents of the first group and
# the value of the variable to the file.
config.txt # name of the file.

You may also change the value after DRIVER, if you want:

sed -iE 's/(DRIVER : )(.*)/\1ODBC/' config.txt

Using sed to update a property in a java properties file

Assuming Linux Gnu sed, 1 solution would be

Edits escaped '.' chars i.e. s/example\.java.../ per correct comment by Kent

sed -i "s/\(example\.java\.property=\).*\$/\1${replaceString}/"

If you're using BSD sed on a Mac for instance, you'll need to supply an argument to the -i to indicate the backup filename. Fortunately, you can use

 sed -i '' "s/\(example\.java\.property=\).*\$/\1${replaceString}/"  

as an argument, and avoid having to manage .bak files in your workflow. (BSD sed info added 2018-08-10)

If your sed doesn't honor the -i, then you have to manage tmp files, i.e.

    sed "s/\(example\.java\.property=\).*\$/\1${replaceString}/" > myTmp
/bin/mv -f myTmp

I hope this helps.

Modify config file using bash script

A wild stab in the dark for modifying a single value:

sed -c -i "s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/" $CONFIG_FILE

assuming that the target key and replacement value don't contain any special regex characters, and that your key-value separator is "=". Note, the -c option is system dependent and you may need to omit it for sed to execute.

For other tips on how to do similar replacements (e.g., when the REPLACEMENT_VALUE has '/' characters in it), there are some great examples here.

How to change ulimit value in limits.conf using sed command?

This bash script will add $LINE_TO_APPEND to the end of limits.conf when the given type and item is not already present in limits.conf:

LINE_TO_APPEND='*               hard    nofile          512'
read -a line_array <<< "$LINE_TO_APPEND"
line_type=${line_array[1]} # consists of just normal characters
line_item=${line_array[2]} # consists of just normal characters
if [[ -z $(grep "$line_type *$line_item" limits.conf) ]]; then
echo "$LINE_TO_APPEND" >> limits.conf

If you want a more purely sed approach, the following less-generic command with hard-coded fields will work (I got caught up in "escaping hell" trying to use $; I'll try revisiting this answer later!):

sed -i 'H;1h;$!d;x;/soft  *nofile/!s/$/\n*   soft   nofile  100000/' limits.conf

Explanation of sed solution:

  • H;1h;$!d;x read entire file into pattern buffer (see sed: read whole file into pattern space without failing on single-line input)
  • /soft *nofile/! if file does not contain soft nofile (arbitrary spacing),
  • s/$/\n* soft nofile 100000/ then add * soft nofile 100000 to the end
  • -i change file in place

How to change file parmater by input in bash

If [[ "$1" == "Y"]]
sed -ri 's/(^.*SELINUX=)(.*$)/\1enforce/' file
sed -ri 's/(^.*SELINUX=)(.*$)/\1permissive/' file

If the first passed parameter ($1) is equal to "Y" use sed to split SELINUX line into to 2 sections. Substitute the line for the first section followed by "enforce". If the passed parameter is not "Y" substitute the line for the first section followed by "permissive".

How to replace a substring inside a file using shell script and match expression?

With sed :

$ sed "s@'1\.3\.3\.1204\.0'@''@;\
s@'1\.3\.3\.1204\.4'@''@" file


  version                : ''
buildVersion : ''


Add -i switch if you want to replace on the fly

Related Topics

Leave a reply