将一个子字符串替换为Shell脚本中的另一个字符串

本文翻译自: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 ,请确保转义每个字符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值