Sahar Rabinoviz
Sahar Rabinoviz

Reputation: 1979

Remove quotes surrounding number

I have a file with the lines looking similar to:

"somestring": "20.000",

I want to remove the quotes around 20.000.

Other use cases are

"somestring": "20",
"somestring": "blahhhah 3",
"somestring": "",

which should give

"somestring": 20,
"somestring": "blahhhah 3",
"somestring": "",

Obvious regex to recognize numbers is [0-9]+ and [0-9].[0-9]+ for decimals. I was told this is possible with the sed command as it can backreference, however I do not know how to use the sed command. Can someone please provide me with any linux command to accomplish this?

Upvotes: 4

Views: 754

Answers (3)

Gilles Quénot
Gilles Quénot

Reputation: 185005

$ sed -r 's@"([0-9]+(\.[0-9]+)?)",\s*$@\1,@' file
"somestring": 20.000,
"somestring": 20,
"somestring": "blahhhah 3",
"somestring": "",

Upvotes: 3

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89547

In basic mode:

sed 's/^\("[^"]*": \)"\([0-9][0-9]*\(\.[0-9]*\)\?\)"/\1\2/' file.txt

Upvotes: 0

anubhava
anubhava

Reputation: 784958

You can use:

sed -i.bak -r 's/"([[:digit:]]+(\.[[:digit:]]+){0,1})"/\1/' file
"somestring": 20,
"somestring": "blahhhah 3",
"somestring": "",
"somestring": 20.000,

On OSX use:

sed -E 's/"([[:digit:]]+(\.[[:digit:]]+){0,1})"/\1/'

Upvotes: 2

Related Questions