djq
djq

Reputation: 15288

Find/Replace using grep and Textwrangler

I'm using grep on TextWrangler to find strings of this type:

4,600.00

My regex command is the following:

\d.\d{3}.\d{2}

which seems to find the strings correctly. I would like to replace the string 4,600.00 with 4600.00 as I wish to save the data in a .csv file. How do I remove the comma from each number that I find?

Upvotes: 7

Views: 20608

Answers (3)

ZZ-bb
ZZ-bb

Reputation: 2167

Search: (\d{1,3}),(\d{1,3})\.(\d{1,2})

Replace: \1\2.\3

Works in TextWrangler.

Upvotes: 12

Steve
Steve

Reputation: 54532

One way using sed:

sed -r 's/([0-9]{1,3}),([0-9]{3}\.[0-9]{2})/\1\2/g' file.txt

You could add the -i flag to write the changes directly to the file.

Upvotes: 0

Kent
Kent

Reputation: 195209

awk oneliner:

awk  --re-interval  '{x=gensub(/([0-9]{1,3}),([0-9]{3}\.[0-9]{2})/,"\\1\\2","g");print x}' file

test:

kent$  awk --version|head -1
GNU Awk 3.1.6

kent$  echo "foo,bar,blah,46,000.00,some,more"|awk --re-interval  '{x=gensub(/([0-9]{1,3}),([0-9]{3}\.[0-9]{2})/,"\\1\\2","g");print x}'
foo,bar,blah,46000.00,some,more

Upvotes: 1

Related Questions