1

I have a file containing multiple similar sections

...
# PostgreSQL configuration example
#production:
#  adapter: postgresql
#  database: redmine
#  host: localhost
#  username: postgres
#  password: "postgres"

# SQLite3 configuration example
#production:
#  adapter: sqlite3
#  database: db/redmine.sqlite3

# SQL Server configuration example
#production:
#  adapter: sqlserver
#  database: redmine
#  host: localhost
#  username: jenkins
#  password: jenkins
...

I would like to comment/decomment an entire section.

With "SQLite3" I would like to have the following output:

...
# PostgreSQL configuration example
#production:
#  adapter: postgresql
#  database: redmine
#  host: localhost
#  username: postgres
#  password: "postgres"

# SQLite3 configuration example
production:
  adapter: sqlite3
  database: db/redmine.sqlite3

# SQL Server configuration example
#production:
#  adapter: sqlserver
#  database: redmine
#  host: localhost
#  username: jenkins
#  password: jenkins
...

I tried something like:

sed -i -e '/SQLite3/+1,/^\s*$/ s/^#//' FILE

... but it doesn't work because '+1' is not valid.

How can I start a range on the line after the match? (I tried several variations with braces and 'n', but I didn't find the right spell)

ZioByte
  • 2,690
  • 1
  • 32
  • 68
  • Please add your desired output for that sample input to your question. – Cyrus Oct 14 '17 at 12:20
  • Possible duplicate of [How to select lines between two patterns?](https://stackoverflow.com/questions/38972736/how-to-select-lines-between-two-patterns) ... modify this form `sed -n '/PAT1/,/PAT2/{/PAT1/!p}' file` – Sundeep Oct 14 '17 at 12:27
  • @Cyrus: updated. – ZioByte Oct 14 '17 at 12:30

1 Answers1

1

Select the complete range, then apply the substitute command only to a subrange of lines not matching the first pattern:

sed '/SQLite3/,/^$/ { /SQLite3/! s/^#// }' file

Expanded for readability:

sed '/SQLite3/,/^$/ {
    /SQLite3/! {
        s/^#//
    }
}' file
randomir
  • 17,989
  • 1
  • 40
  • 55