-1

I need a tool or one-liner or whatever, which will export the entire server {...} block containing the given domain.

For examples:

### 1)
cat conf.d/domains.conf
server {

  include http.conf;
  server_name example.com;

  # [...]

}

server {

  include https.conf;
  server_name foo.com;

  # [...]

}

server {

  include https.conf;
  server_name example.com;

  # [...]

}

---

./script conf.d/domains.conf example.com

server {

  include http.conf;
  server_name example.com;

  # [...]

}

server {

  include https.conf;
  server_name example.com;

  # [...]

}

### 2)
cat conf.d/domains.conf

server {

  include ssl.conf;
  server_name example-foo.com;

  # [...]

}

server {

  include ssl.conf;
  server_name example-bar.com
              example.com
              fake.domain.com;

  location / {
    return 204 "OK";
  }

  # [...]

}

server {

  # include headers.conf;
  include ssl.conf;

  server_name example-baz.com example.com;

  # [...]

}

server {
  include ssl.conf;
  root /var/www/;

  server_name example-baz.com exampleee.com;

  # [...]

}

---

./script conf.d/domains.conf example.com

server {

  include ssl.conf;
  server_name example-bar.com
              example.com;

  # [...]

}

server {

  include ssl.conf;
  server_name example-bar.com
              example.com
              fake.domain.com;

  location / {
    return 204 "OK";
  }

  # [...]

}

server {

  # include headers.conf;
  include ssl.conf;

  server_name example-baz.com example.com;

  # [...]

}

My config has a lot of lines and is divided into many external files.

Do you know the tools that do this? Or any suggestions?

Thanks!

  • 1
    Welcome to U&L at stack exchange. What have you already tried to achieve your goal? The user community here is much more likely to engage and help if you show what specific problems you are having trying to succeed, rather than just asking someone to provide an answer. – 0xSheepdog Nov 12 at 16:05
1

You can use awk for this by filtering two patterns between a delimiter, in your case a domain and a curly brace '$domain {'

E.g:

awk '/exampledomain  {/,/}/' exampledomains.conf

This command will take the first delimiter:

exampledomain {

and the second delimiter, the closing curly brace:

}

And print out the contents including the domain line from a file of your choosing, in this example: exampledomains.conf

Please take into account the the input has to be literal for this to work, including empty spaces.

  • Thanks, but it doesn't work. I update my question for more information (see example 2). – trimstray Nov 12 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.