Bash 删除字符串中出现的小词

Bash 删除字符串中出现的小词,bash,shell,Bash,Shell,我正在尝试从字符串中删除特定的单词。我无法将“the”替换为空字符串,因为“the”可能是字符串中单词的一部分 word: "the" string: "the_ad_an_feta_cfr_era_the_iop_the" output: "ad_an_feta_cfr_era_iop" 这个词可以是开头,在字符串的中间或结尾的几次,所以我必须考虑字符串的分隔符和开始/结束。 我可以用一个正则表达式来处理所有这些问题,还是应该使用循环,但如何在sed中指定多个模式呢 sed 's/the//

我正在尝试从字符串中删除特定的单词。我无法将“the”替换为空字符串,因为“the”可能是字符串中单词的一部分

word: "the"
string: "the_ad_an_feta_cfr_era_the_iop_the"
output: "ad_an_feta_cfr_era_iop"

这个词可以是开头,在字符串的中间或结尾的几次,所以我必须考虑字符串的分隔符和开始/结束。 我可以用一个正则表达式来处理所有这些问题,还是应该使用循环,但如何在sed中指定多个模式呢

sed 's/the//g' <<< "the_ad_feta_cfr_era_the_iop_the"

看看这个
sed

$ string='the_ad_an_feta_cfr_era_the_iop_the'
$ sed -E -e ':a' -e 's/(^|_)(the|an|is|feta)(_|$)/\1/g;ta' -e 's/_$//' <<< "$string"
ad_cfr_era_iop
$ string='the_ad_an_feta_cfr_era_the_iop_the'
$ sed -E -e ':a' -e 's/(^|_)(the|an|is|feta)(_|$)/\1/g;ta' -e 's/_$//' <<< "$string"
ad_cfr_era_iop
$ string='the_ad_an_feta_cfr_era_the cfr_the_iop_the'
$ sed -E -e 's/_/__/g;s/(^|_)(the|an|is|feta)(_|$)//g;s/_+/_/g;s/^_//;s/_$//' <<< "$string"
ad_cfr_era_the cfr_iop