简体   繁体   中英

SED one liner to uncomment and replace first occurrence of a pattern

I have this settings.conf file in linux defined as follows:

Section A
  first-setting = value
#  second-setting = off
  third-setting = value

Section B
  first-setting = value
#  second-setting = off
  third-setting = value

I would like to uncomment # second-setting = off of Section A only (first occurrence), and set the value to on.

So far, I have this:

cat settings.conf | sed '/^# second.*/ {s/^#//;s/off/on/}'

Any tips?

Is this what you had in mind?

sed '0,/#/s/#\(.*\) off/\1 on/' settings.conf

Or if your on osx with non-gnu sed:

sed '1,/#/s/#\(.*\) off/\1 on/' settings.conf
sed -e '1,/^Section B/s/#  second-setting = off/  section-setting = on/' settings.conf

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