Crazy_Bash
Crazy_Bash

Reputation: 1935

Remove numbers within brackets only

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'

Upvotes: 1

Views: 356

Answers (3)

user529758
user529758

Reputation:

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'

Upvotes: 3

anubhava
anubhava

Reputation: 784958

You can use:

On Mac:

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

On Linux:

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

Upvotes: 4

doubleDown
doubleDown

Reputation: 8398

This should work (added the escaped +)

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

Upvotes: 1

Related Questions