【发布时间】:2018-02-26 21:23:36
【问题描述】:
在一个 bash 脚本(新手到 bash)中,我有一个像这样的变量:"this is a message [REL]",我想把它转换成这个:"this is a message"。
我尝试了原生 bash 字符串操作,例如 CLEAN_MESSAGE=${MESSAGE#REL},但没有成功,所以我尝试使用 sed,我关注了一些类似这样的帖子:How to replace paired square brackets with other syntax with sed? 但没有运气
这是我目前的状态:
MESSAGE=$(cat $1) #A message with a tag [REL]
RELTAG=\[[REL]\] #A variable with regexp for tag, tryed \[REL\], \[(REL)\], \[\(REL\)\] and other variants
CLEAN_MESSAGE=$(echo "$MESSAGE" | sed "s/$RELTAG//")
echo $CLEAN_MESSAGE
对于 RELTAG 的某些变体,我得到 EL] 或 [RE,但从未删除完整的标签。
帮助?
【问题讨论】:
-
试试
sed 's/\[[^]]*\]//'