ZioByte
ZioByte

Reputation: 2994

sed range starting at line after match and ending on second match

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)

Upvotes: 1

Views: 415

Answers (1)

randomir
randomir

Reputation: 18687

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

Upvotes: 1

Related Questions