简体   繁体   中英

Android shell sed trouble

I'm trying to use sed to remove all instances of text enclosed by square brackets from a document, so my code is:

sed -r 's|\[[^\]]*\]||g' file

However, this does not seem to pick up all instances of text enclosed by square brackets. In particular, it seems to fail with:

[/a]
[/b]
[span style "blah" blah]

and so forth, but it works with square brackets enclosing plaintext. As far as I can tell, the regex should be looking for square brackets enclosing any character that is not a ], so what exactly is wrong?

I tried this regex and it didn't remove any text in brackets.

When I removed that second '\\', so the regex was

 sed -r 's|\[[^]]*\]||g'

it worked perfectly in all cases.

I tried it on both Mac and Linux. Android runs Linux, so it will probably work there too.


A word of explanation: Scansets are special where escapes are concerned. If the ']' character is the very first one in the scanset, you don't need to escape it. If it's not the very first one in the scanset, you can't escape it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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