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
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 as1e5
or3d7
. – 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