Pixeluz
Pixeluz

Reputation: 267

BASH - How to delete all numerals from a text file, unless they are part of a specific string?

I have a text file, and I want to delete all the numerals included in them. However, there are two key strings "9/11" and "September 11", in which I want to keep the numerals. How can I delete all the numerals except when they are a part of these key strings?

I use sed 's/[0-9]*//g' to get rid of the numerals. So for now, the sample text before processing would be something like this:

12 Aug. 2002, News Section. 9/11 was a terrible tragedy for the nation, in which 2,500 ...

And I want the file after processing to look like this:

Aug. , News Section. 9/11 was a terrible tragedy for the nation, in which ...

I tried searching for the answer, but to no avail. Thanks in advance for any suggestions.

Upvotes: 1

Views: 71

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174874

This will do the job. It's like a kind of capturing the part we want to stay and matching the part you want to remove. So by replacing all the matched characters with the chars present inside group index 1 will make the captured chars to stay and the other matched chars to leave.

sed 's~\(\b9/11\b\|\bSeptember 11\b\)\|[[:digit:]]~\1~g' file

DEMO

Upvotes: 1

Related Questions