加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

从字符串bash中删除一个单词

发布时间:2020-12-16 01:50:21 所属栏目:安全 来源:网络整理
导读:我有字符串 file="this-is-a-{test}file" 我想从此字符串中删除{test}. 我用了 echo $file | sed 's/[{][^}]*//' 但是这让我感动了 this-is-a-}file 我怎么能删除}? 谢谢 解决方法 你可以使用正确的正则表达式sed: s="this-is-a-{test}file"sed 's/{[^}]*}
我有字符串

file="this-is-a-{test}file"

我想从此字符串中删除{test}.
我用了

echo $file | sed 's/[{][^}]*//'

但是这让我感动了

this-is-a-}file

我怎么能删除}?

谢谢

解决方法

你可以使用正确的正则表达式sed:

s="this-is-a-{test}file"
sed 's/{[^}]*}//' <<< "$s"
this-is-a-file

或者这个awk:

awk -F '{[^}]*}' '{print $1 $2}' <<< "$s"
this-is-a-file

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读