本文翻译自:Replace one substring for another string in shell script
I have "I love Suzi and Marry" and I want to change "Suzi" to "Sara". 我有“我爱Suzi和结婚”,我想将“ Suzi”更改为“ Sara”。
#!/bin/bash
firstString="I love Suzi and Marry"
secondString="Sara"
# do something...
The result must be like this: 结果必须是这样的:
firstString="I love Sara and Marry"
#1楼
参考:https://stackoom.com/question/tQki/将一个子字符串替换为Shell脚本中的另一个字符串
#2楼
尝试这个:
sed "s/Suzi/$secondString/g" <<<"$firstString"
#3楼
To replace the first occurrence of a pattern with a given string, use ${ parameter / pattern / string }
: 要用给定的字符串替换第一次出现的模式,请使用${ parameter / pattern / string }
:
#!/bin/bash
firstString="I love Suzi and Marry"
secondString="Sara"
echo "${firstString/Suzi/$secondString}"
# prints 'I love Sara and Marry'
To replace all occurrences, use ${ parameter // pattern / string }
: 要替换所有出现的内容,请使用${ parameter // pattern / string }
:
message='The secret code is 12345'
echo "${message//[0-9]/X}"
# prints 'The secret code is XXXXX'
(This is documented in the Bash Reference Manual , §3.5.3 "Shell Parameter Expansion" .) (这在Bash参考手册的第 3.5.3节“ Shell参数扩展”中进行了介绍 。)
Note that this feature is not specified by POSIX — it's a Bash extension — so not all Unix shells implement it. 请注意,POSIX未指定此功能-它是Bash扩展-因此并非所有Unix Shell都实现此功能。 For the relevant POSIX documentation, see The Open Group Technical Standard Base Specifications, Issue 7 , the Shell & Utilities volume, §2.6.2 "Parameter Expansion" . 有关相关的POSIX文档,请参见《 Open Group技术标准基本规范》第7期 , Shell&Utilities卷,第2.6.2节“参数扩展” 。
#4楼
This can be done entirely with bash string manipulation: 这可以完全通过bash字符串操作来完成:
first="I love Suzy and Mary"
second="Sara"
first=${first/Suzy/$second}
That will replace only the first occurrence; 那将仅替换第一次出现; to replace them all, double the first slash: 要全部替换,请将第一个斜杠加倍:
first="Suzy, Suzy, Suzy"
second="Sara"
first=${first//Suzy/$second}
# first is now "Sara, Sara, Sara"
#5楼
For Dash all previous posts aren't working 对于Dash,以前的所有帖子均无效
The POSIX sh
compatible solution is: POSIX sh
兼容解决方案是:
result=$(echo "$firstString" | sed "s/Suzi/$secondString/")
#6楼
It's better to use bash than sed
if strings have RegExp characters. 如果字符串具有RegExp字符,则使用bash比sed
更好。
echo ${first_string/Suzi/$second_string}
It's portable to Windows and works with at least as old as Bash 3.1. 它可移植到Windows,并且至少与Bash 3.1一样老。
To show you don't need to worry much about escaping let's turn this: 为了表明您不必担心转义,我们将其转为:
/home/name/foo/bar
Into this: 变成这个:
~/foo/bar
But only if /home/name
is in the beginning. 但仅当/home/name
开头时。 We don't need sed
! 我们不需要sed
!
Given that bash gives us magic variables $PWD
and $HOME
, we can: 鉴于bash为我们提供了魔术变量$PWD
和$HOME
,我们可以:
echo "${PWD/#$HOME/\~}"
EDIT: Thanks for Mark Haferkamp in the comments for the note on quoting/escaping ~
.* 编辑:谢谢马克·哈弗坎普(Mark Haferkamp)在注释中引述/转义~
。*
Note how the variable $HOME
contains slashes but this didn't break anything. 注意变量$HOME
如何包含斜杠,但这并没有破坏任何东西。
Further reading: Advanced Bash-Scripting Guide . 进一步阅读: 高级Bash脚本指南 。
If using sed
is a must, be sure to escape every character . 如果必须使用sed
,请确保转义每个字符 。