I\'m trying to write a simple Bash script. I have a simple \"template\" variable:
template = \"my*appserver\"
I then have a function (
Bash can do string replacement by itself:
template='my*appserver'
server='live'
template="${template/\*/$server}"
See the advanced bash scripting guide for more details on string replacement.
So for a bash function:
function string_replace {
echo "${1/\*/$2}"
}
And to use:
template=$(string_replace "$template" "$server")
String replacement in a bash-script can e.g. be achieved by sed:
template=$(echo $template | sed 's/old_string/new_string/g')
This will replace old_string with new_string in the template variable.
I needed to do something like this, but I needed sed and the replace clause needed to include a variable. I ended up with this.
Where the variable name is $puttyline
sed "s/\(\[remote .origin.\]\)/\1\n$puttyline/"
So sed searches for [remote "origin"]
and remembers the match, and inserts a line right after, containing whatever's in the variable $puttyline
.
This can get ugly however if $puttyline contains any special characters that sed would react to, like \ or $. You have to either escape them prior with another call to sed, or... do something smarter in bash that I'm too crappy with bash to know. For example, to double-escape all backslashes:
sed 's/\\/\\\\/g'
Based on @Spencer Rathbun
response, this can also be done this way:
function string_replace {
#DOC: "${string/match/replace}"
string=$1
echo "${string/$2/$3}"
}
template='my__patternReplacing__appserver'
match='__patternReplacing__'
replace='live'
template=$(string_replace "$template" "$match" "$replace")
There are so many posts available online but the one which worked fine for me is as below.
There are 2 ways to achieve this, if you want to replace a string (my old string is with special character :) with another string in a file, then use this:
sed -i '/oldtext:/c\newtext: Rahul' ./printName.txt
Secondly, if you want to search for a string and then replace it with your variable, then do this:
#here im concatinating two key + value(myvariable).
firstkey="oldtext: old"
key="newtext: "
value=$(cat ./variableStoredHere.txt)
key+=$value
result for below command will be oldtext: old
echo $firstkey
result for below command will be will be newtext: Rahul
echo $key
sed -i "s/$firstkey/$key/g" ./printName.txt
As nobody mentioned it, here's a cool possibility using printf
. The place-holder must be %s
though, and not *
.
# use %s as the place-holder
template="my%sappserver"
# replace the place-holder by 'whatever-you-like':
server="whatever-you-like"
printf -v template "$template" "$server"
Done!
If you want a function to do that (and notice how all the other solutions mentioning a function use an ugly subshell):
#!/bin/bash
# This wonderful function should be called thus:
# string_replace "replacement string" "$placeholder_string" variable_name
string_replace() {
printf -v $3 "$2" "$1"
}
# How to use it:
template="my%sappserver"
server="whatever-you-like"
string_replace "$server" "$template" destination_variable
echo "$destination_variable"
Done (again)!
Hope you enjoyed it... now, adapt it to your needs!
Remark. It seems that this method using printf
is slightly faster than bash's string substitution. And there's no subshell at all here! Wow, that's the best method in the West.
Funny. If you like funny stuff you could write the function string_replace
above as
string_replace() {
printf -v "$@"
}
# but then, use as:
string_replace destination_variable "$template" "$server"