ebvogt
ebvogt

Reputation: 43

How to remove all characters but dots and numbers

I need to clear all characters but numbers and dots in a file.

The numbers are formatted as follows:

$(24.50)

Im using the following code to accomplish the task:

sed 's/[^0-9]*//'

It works but the last parenthesis is not removed. After running the code i get:

24.50)

I should get:

24.50

Please help

Upvotes: 0

Views: 1486

Answers (2)

Mike Holt
Mike Holt

Reputation: 4612

Your regular expression is only matching a single instance of [^0-9.]*. Namely, the $( at the beginning. In order to get sed to match and replace all instances, you need to put a g at the end, as in:

sed 's/[^0-9.]*//g'

The g basically means "match this regular expression anywhere in the input". By default, it will only match on the first instance it encounters, and then stop.

Upvotes: 1

Richard St-Cyr
Richard St-Cyr

Reputation: 995

I think you could use the following:

sed 's/[^0-9.]//g'

Upvotes: 1

Related Questions