Use sed command to replace , appearing between numbers

3

I have a CSV file where data are in the following format

|001|,|abc,def|,123456,789,|aaa|,|bbb|,444,555,666

I want to replace only those "," that appears between numbers with some other character like say SOH or $ or *

other "," appearing in the line should not get replaced i.e. to say I wish to have following output

|001|,|abc,def|,123456*789,|aaa|,|bbb|,444*555*666

Can someone please help me with sed command pattern to get the above desired output

Saurabh

Posted 2010-12-20T13:19:40.337

Reputation: 31

Answers

4

Try this

sed 's/\([0-9]\),\(-\?[0-9]\)/\1\*\2/g'

The first section matches a digit followed by a comma followed by a digit or a -. The next section simply regurgitates the first digit, the replacement character, and then the last digit.

This will catch any pattern like "#,#" or "#,-#", including some that may not be desired (e.g. "abc123,-def" --> "abc123*-def"). Fixing this requires more knowledge of the input stream. (see the comments for details.)

jwernerny

Posted 2010-12-20T13:19:40.337

Reputation: 951

Thanks for the Reply. It almost works !! BUT breaks when "," is followed by a Negative Number i.e. If data is \n |001|,|abc,def|,123456,789,|aaa|,|bbb|,444,555,-666,777 \n It converts to |001|,|abc,def|,123456789,|aaa|,|bbb|,444555,-666777 Desired output is |001|,|abc,def|,123456789,|aaa|,|bbb|,444555-666*777 – Saurabh – 2010-12-21T07:04:18.330

I updated the answer to add handling of a minus sign on the second number. – jwernerny – 2010-12-21T12:36:44.017

@jwernemy As written, your pattern will also match 5,-hi!-. The solution would be to replace the number pattern with a more general numeric pattern. Requiring that the number be delimited by , on both ends would also avoid capturing mixed alphanumeric entries like ,abc123,-45ok,. The number pattern itself would be something like \(\(-\|+\)?[0-9]+\.?[0-9]*\); this handles sign-optional floating point representations, though not scientific-notation style numbers such as 1e5 or 3d7. – Jeremy W. Sherman – 2010-12-21T20:17:42.537

@Jeremey - I see the issue of failing the case "123,-abc", so I will correct it. The other cases you hint at are going to be a problem because we don't know how far backwards or forwards to look for a number because we don't know the field delimiters. – jwernerny – 2010-12-22T15:17:11.307

0

Following seems to work :

sed 's/\([0-9]\),\([-0-9]\)/\1\*\2/g'

Saurabh

Posted 2010-12-20T13:19:40.337

Reputation: 31