简体   繁体   中英

shell sed - substitute an unknown string between a known string and a generic delimiter

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!

这将适用于值中的一个或没有引用的部分 (引用部分之前和之后的未引用部分):

sed -i '/software/ s/setting:[^,"]*("[^"]*")?[^,"]*,/setting:,/' $fileName
echo 'software=setting:"value1,value2",setting2:value,setting3:value'|sed -E '/software/ s/setting:("[^"]*"|[^,"]*),/setting:,/'

这与OSX上的sed一样,gnu sed的选项略有不同。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM