How to Use Sed to Replace a String in a File with a Shell Variable

how to replace a variable in shell script string

You are missing the end of that single-quote pair in your script.

Change from:

echo $SQL | sed -e "s/'$BATCH_END/$BATCH_END/g"

To:

echo $SQL | sed -e "s/\$BATCH_END/$BATCH_END/g"

Updated - as per followup comment:

To save the result of the above replacement back into $SQL, do either of the following:

# Preferred way
SQL=$(echo $SQL | sed -e "s/\$BATCH_END/$BATCH_END/g")

# Old way
SQL=`echo $SQL | sed -e "s/\$BATCH_END/$BATCH_END/g"`

This is called command substitution. Either syntax ($(...) vs. enclosure by backticks) works, but the preferred one allows you to do nesting.

The preferred-preferred way: Herestring

This is probably a bit more advanced than what you care about, but doing it in the following way will save you a subprocess from having to use echo unnecessarily:

SQL=$(sed -e "s/\$BATCH_END/$BATCH_END/g" <<< $SQL)

Replace string variable with string variable using Sed

You need to escape your oldline so that it contains no regex special characters, luckily this can be done with sed.

old_line=$(echo "${old_line}" | sed -e 's/[]$.*[\^]/\\&/g' )
sed -i -e "s/${old_line}/${new_line}/g" ethernet

how to use sed to replace a string in a file with a shell variable

It seems to be that you're trying a combination of piping to sed and using a file. The following will work perfectly fine,

sed -i -e "s/{VERSION}/${VERSION}/" -e "s/{DISTRO}/${DISTRO}/" ${OUT_CAT}

You don't need to escape the $, and you don't pipe into sed since you're using -i to modify the file in-place. You also need to use -e for each expression when their are more than one.

EDIT: Here is the bit in the sed manual pages that gives the indication of the issue with -e option (emphasis mine),

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret. All
remaining arguments are names of input files
; if no input files are
specified, then the standard input is read.

Environment variable substitution in sed

Your two examples look identical, which makes problems hard to diagnose. Potential problems:

  1. You may need double quotes, as in sed 's/xxx/'"$PWD"'/'

  2. $PWD may contain a slash, in which case you need to find a character not contained in $PWD to use as a delimiter.

To nail both issues at once, perhaps

sed 's@xxx@'"$PWD"'@'

How to replace a string in a variable with another string in a file using shell script

The default delimiter of sed clashes with the same character's present in your WORKPLACE. As mentioned in the comments, changing the delimiter to a character not present in any of your input or escaping the slashes would work for that issue.

However, you also have a single quoting issue. Not only would you get an error, the variables will not expand.

This sed should work

$ sed -i.bak s"|$WORKSPACE|/home/Projects/XXX/$PASE_PRODUCT/common/XXX-$SUB_PRODUCT|" polyspaceFiles_"$PASE_SUB_PRODUCT"_tmp.opts

Find and replace inside a string variable using sed command - shell scripting

You don't use -i, as that's for changing a file in place. If you want to replace the value in the variable, you need to reassign to it.

How to replace variable string with another variable string using sed

replace a line content in a file with multiple lines

In sed commands are delimetered with a newline. So when sed sees s/blabla/blabla<newline> is parses that part as a full command and exits with a missing closing /.

You can substitute each newline in the replacement string for two characters \n, which can be then substituted by sed for a newline once again.

string2=${string2//$'\n'/\\n}
sed "s/$string1/$string2/"

Note that sed parses first part of s command as a regex and in replacement string some strings (\1 & \L \U etc.) are also parsed specially.

This will only work if there are no newlines in string1. With GNU sed you can get away with newlines in string1 by using -z option, that will cause to parse the input as zero terminated strings.

Can I use sed to manipulate a variable in bash?

Try this:

website=$(sed 's|/|\\/|g' <<< $website)

Bash actually supports this sort of replacement natively:

${parameter/pattern/string} — replace the first match of pattern with string.

${parameter//pattern/string} — replace all matches of pattern with string.

Therefore you can do:

website=${website////\\/}

Explanation:

website=${website // / / \\/}
^ ^ ^ ^
| | | |
| | | string, '\' needs to be backslashed
| | delimiter
| pattern
replace globally

Use sed to replace pattern and insert variable on the inserted string

You need to use double quotes in order for the bash variable to be interpolated in the sed command. As you have double quotes in your replacement string you need to escape those:

cat grub.text
GRUB_CMDLINE_LINUX="random_text_here"
GRUB_HG=53737 sed -i "s|\GRUB_CMDLINE_LINUX=.*|GRUB_CMDLINE_LINUX=\"hugepages=${GRUB_HG} processor.max_cstates=1 idle=poll pcie_aspm=off intel_iommu=on\"|" grub.text
cat grub.text
GRUB_CMDLINE_LINUX="hugepages=53737 processor.max_cstates=1 idle=poll pcie_aspm=off intel_iommu=on"

Unable to use sed to replace text with shell variable

try taking the 's off the sed e.g

$ new=N
$ cat > j
one
two
three
$ sed -e "s/one/${new}/" j
N
two
three

for a more complete answer try this answer



Related Topics



Leave a reply



Submit