Reputation: 1979
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
Reputation: 185005
$ sed -r 's@"([0-9]+(\.[0-9]+)?)",\s*$@\1,@' file
"somestring": 20.000,
"somestring": 20,
"somestring": "blahhhah 3",
"somestring": "",
Upvotes: 3
Reputation: 89547
In basic mode:
sed 's/^\("[^"]*": \)"\([0-9][0-9]*\(\.[0-9]*\)\?\)"/\1\2/' file.txt
Upvotes: 0
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