1

How can I remove numbers within brackets only? For example, I have the following text:

This is 14 April [988] text..

I would like to remove the [988] and leave 14 intact.

What i've tried so far:

sed 's@\[[0-9]\]@@g'
Crazy_Bash
  • 1,755
  • 10
  • 26
  • 38
  • 3
    So close! `sed 's@\[[0-9]*\]@@g'` – Paul Tomblin Oct 16 '12 at 19:12
  • It's funny how people don't get this especially today. http://stackoverflow.com/questions/12921217/group-more-than-one-regular-expression-in-the-same-command/12921253#12921253 –  Oct 16 '12 at 19:16

3 Answers3

4

You can use:

On Mac:

sed -E 's@\[[0-9]+\]@@g'

On Linux:

sed -r 's@\[[0-9]+\]@@g'
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • +1 for you, best answer so far. But come on, don't downvote me choking on `sed`'s flags! –  Oct 16 '12 at 19:22
  • 1
    @H2CO3: I haven't downvoted any answer (I very rarely down vote any answer, I merely comment to let people know) – anubhava Oct 16 '12 at 19:24
3

You need to enable repeating (multiple) numerical characters in order to get this work - as it stands, your regex will remove only single-digit numbers. If you want numbers composed of at least one (one or more) numerical characters, try

sed -E 's/\[[0-9]+\]//g'
  • @anubhava probably the text to be replaced contains slashes? What if you try with your original `@` delimiter? (I just use a slash because I'm used to it.) –  Oct 16 '12 at 19:20
  • Original text is: `'This is 14 April [988] text'` – anubhava Oct 16 '12 at 19:21
  • Let's see after edit: http://dl.dropbox.com/u/4960980/Screen%20Shot%202012-10-16%20at%209.26.03%20PM.png –  Oct 16 '12 at 19:26
1

This should work (added the escaped +)

sed 's@\[[0-9]\+\]@@g'
doubleDown
  • 8,048
  • 1
  • 32
  • 48