开发者

Help! How do you s/// "[one] abc [two]" into "X abc X"?

开发者 https://www.devze.com 2023-02-19 10:57 出处:网络
My failed attempt: s/\\[[^\\[\\]]*\\]/X/ No mat开发者_JS百科ch in ed or sed. Thanks!If you mean the literal string [one] abc [two] use e.g. this:

My failed attempt:

s/\[[^\[\]]*\]/X/

No mat开发者_JS百科ch in ed or sed.

Thanks!


If you mean the literal string [one] abc [two] use e.g. this:

s/[[][^]]*]/X/g

It'll replace [ + anything not ] + ] with X.


The [brackets] literally? Or is the blank the significant part?

sed -r 's/(.*) (.*) (.*)/X \2 X/'

echo "[one] abc [two]" | sed -r 's/(.*) (.*) (.*)/X \2 X/'
X abc X


Ruby(1.9+)

$ echo "[one] abc [two]" | ruby -e 'print gets.gsub(/\[.*?\]/,"X") '
X abc X
0

精彩评论

暂无评论...
验证码 换一张
取 消