繁体   English   中英

sed regex:从字符串中删除[和]的出现

[英]sed regex : remove occurrences of [ and ] from string

我正在使用Linux Mint 17。

我可以替换测试字符串中的所有内容,但[] (方括号的开头和结尾)除外。

这是我的表达式,位于bash脚本中,该脚本是从命令行运行的

video_title=$(echo $video_title | sed 's|[?![]]||g')

我尝试将\\放在两个方括号之前,但这不起作用。 如果我删除[]则表达式替换? ! 正好。

有任何想法吗?

根据手册,要在列表中包含] ,它必须是第一个字符。

 A leading `^' reverses the meaning of LIST, so that it matches any
 single character _not_ in LIST.  To include `]' in the list, make
 it the first character (after the `^' if needed), to include `-'
 in the list, make it the first or last; to include `^' put it
 after the first character.

所以尝试这样的事情:

$ echo '[!2015?]' | sed 's|[][?!]||g'
2015

仅从输入tr删除字符是比sed更合适的工具。 就您而言,您可以使用

video_title=$(echo $video_title | tr -d '?![]')

你可以试试看

$ echo 'foo?![bar]' | sed 's~[?!]\|\]\|\[~~g'
foobar

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM