Sed Replacement Not Working When Using Variables

Sed replacement not working when using variables

Try

find /home/loni/config -type f -exec sed -i "s/${PATTERN}/${REPLACEMENT}/g" {} \;

instead. The ' quotes don't expand variables.

why i can't replace string with variable with sed

Your double quotes are delimiting the shell strings, not being used literally in the sed pattern and replacement.

Put single quotes around the sed operation, except for the variable.

sed -i 's+"t_1", "t_2"+"'"$t1"'", "AAA"+g' test.txt

sed not working correctly when using variables

Consider what happens when the substitution occurs. The command becomes:

sed -i 's/<!--jta-data-source>jdbc/FCBDataSource</jta-data-source-->/<jta-data-source>jdbc/FCBDataSource</jta-data-source>/g' some_filename

Then, sed sees s/<!--jta-data-source>jdbc/FCBDataSource</j… and thinks that you want to replace an occurrence of <!--jta-data-source>jdbc with the text FCBDataSource<, and the s command has an illegal j modifier (and other junk).

You need to pick a delimiter character that does not appear in either the pattern or the replacement text. A , will do.

sed does not replace the string correctly when using a variable

As others recommended, no less command required, and if you need show the line of Local Site Name only, use -n option in Sed.

Second, put varies in braces, it should fix your problem.

site=$(su admin -c get_local_site | sed -n 's/Local Site Name: //p')
sed -i "s/RecoverPoint_[[:alnum:]]*_RPA/RecoverPoint_${site}_RPA/" fakehostfile

Variables not working in sed command

First, you need to use double quotes to allow shell parameters/variables to expand. Then, you need to use braces to isolate the variable name if text that follows the variable could be interpreted as part of variable name (before${var}after). Finally, to use literal $ under double quotes, you should escape it a blackslash. All together:

var=3
sed -i "${var}s/\$/ newvalue/" filename

One alternative is to use alternating double and single quotes (under which no character is treated specially, including $ for parameter expansion):

sed -i "$var"'s/$/ newvalue/' filename

sed substitution is not working with variable

sed -ri "3s/[[:digit:]]+\.json/$rjc1.json/" file

On the third line, substitute, one or more digits and then ".json" for the rjc1 variable followed by ".json"

Use a variable as replacement in bash sed command

TL;DR:

Try:

sed -i '$ s@$@ '"$1"'@' "$DIR./result/doc.md"

Long version:

Let's start with your original code:

sed -i '$ s/$/ /replacement/' "$DIR./result/doc.md"

And let's compare it to the code you referenced:

sed -i '$ s/$/abc/' file.txt

We can see that they don't exactly match up. I see that you've correctly made this substitution:

file.txt --> "$DIR./result/doc.md"

That looks fine (although I do have my doubts about the . after $DIR ). However, the other substitution doesn't look great:

abc -->  /replacement

You actually introduced another delimeter /. However, if we replace the delimiters with '@' we get this:

sed -i '$ s@$@ /replacement@' "$DIR./result/doc.md"

I think that the above is perfectly valid in sed/bash. The $@ will not be replaced by the shell because it is single quoted. The $DIR variable will be interpolated by the shell because it is double quoted.

Looking at one of your attempts:

sed -i "$ s@$@ $1@" "$DIR./result/doc.md"

You will have problems due to the shell interpolation of $@ in the double quotes. Let's correct that by replacing with single quotes (but leaving $1 unquoted):

sed -i '$ s@$@ '"$1"'@' "$DIR./result/doc.md"

Notice the '"$1"'. I had to surround $1 with '' to basically unescape the surrounding single quotes. But then I surrounded the $1 with double quotes so we could protect the string from white spaces.

sed command containing variable not working

# You don't need to (and in your case, should not) invoke date command twice.
# Try running following in bash to see the problem.
# $ echo $(date '+%s')_$(sleep 1)_$(date '+%s')
# On a different note, it's better to use $(...) instead of backticks.
sessionFolderName="/session_$(date '+%y%m%d_%H%M')"

# You can use several other separators in sed.
# e.g. :, ;, @, #, _ and even a space
sed "s:sessionFolder=.*:sessionFolder=${sessionFolder}:" /home/pi/scripts/settings/settings.sh > tmp
mv tmp /home/pi/scripts/settings/settings.sh

Refer to this regarding using $() instead of backticks



Related Topics



Leave a reply



Submit