可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm trying to write a simple Bash script. I have a simple "template" variable:
template = "my*appserver"
I then have a function (get_env()
) that returns the values dev
, qa
, or live
. I'd like to call get_env
and then string-replace the template
variable with get_env
's return value and swap it out with the asterisk. So:
# Returns "dev"
server = get_env
# Prints "mydevappserver"
template = string_replace(server, template)
Or:
# This time, it returns "live"
server = get_env
# Prints "myliveappserver"
template = string_replace(server, template)
What should I be using in lieu of this string_replace()
function to accomplish the binding?
回答1:
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")
回答2:
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.
回答3:
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"
回答4:
Yeah, either 'sed' as the others have said, or start with your template in 2 separate variables and construct on the fly. e.g.
templateprefix="my"
templatesuffix="appserver"
server=get_env
template=${templateprefix}${server}${templatesuffix}
回答5:
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")
回答6:
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'
回答7:
Here's a bash script that wraps this up
Example:
$ search_and_replace.sh "test me out" "test me" ez
ez out
search_and_replace.sh
#!/bin/bash
function show_help()
{
echo ""
echo "usage: SUBJECT SEARCH_FOR REPLACE_WITH"
echo ""
echo "e.g. "
echo ""
echo "test t a => aesa"
echo "'test me out' 'test me' ez => ez out"
echo ""
exit
}
if [ "$1" == "help" ]
then
show_help
exit
fi
if [ -z "$3" ]
then
show_help
exit
fi
SUBJECT=$1
SEARCH_FOR=$2
REPLACE_WITH=$3
echo "$SUBJECT" | sed -e "s/$SEARCH_FOR/$REPLACE_WITH/g"
回答8:
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