0

Ok, so I know there is a similar post that I referred to already but does not fit the exact issue I am having.

For reference: replace a unknown string between two known strings with sed

I have a file with software=setting:value,setting2:value,setting3:value, etc...

My first attempt was to use the reference above with sed -i "/software/ s/setting:.*,/setting:,/" $fileName

However the wildcard references the last comma for that line, not the comma immediately following the match.

My current work around is: sed -i "/software/ s/setting:[^,]*,/setting:,/" $fileName but this limits the ability to have a potential value that contains a comma wrapped inside quotations, etc... I know this is an unlikely scenario but I would like to have an ideal solution where the value can contain any character it would like and just to do a string replacement between the "setting:" and the comma immediately following the value of that particular setting.

Any help is appreciated. Thanks in advance!

Community
  • 1
  • 1
  • So you want to match everything up to the first comma which is not inside a quoted string? Where can the quotes appear? Where can the quoting start and end? Just at the beginning and the end of the value or also in the middle? Can it start and end several times? – Alfe Feb 26 '14 at 23:46

2 Answers2

2

This will work for one or none quoted part in the value (unquoted parts before and after the quoted part):

sed -i '/software/ s/setting:[^,"]*("[^"]*")?[^,"]*,/setting:,/' $fileName
Alfe
  • 56,346
  • 20
  • 107
  • 159
0
echo 'software=setting:"value1,value2",setting2:value,setting3:value'|sed -E '/software/ s/setting:("[^"]*"|[^,"]*),/setting:,/'

This is with sed on OSX, gnu sed has slightly different options.

bazzargh
  • 1,792
  • 10
  • 16