1

I need to add the contents of a text file between certain placeholders in another text file. (Specifically, I'm trying to get around the nginx include limitation inside upstream blocks.)

My main nginx configuration file /etc/nginx/nginx.conf looks like this:

## START UPSTREAM

## END UPSTREAM

http {
...
}

My server upstream file /etc/nginx/upstream.conf looks like this:

upstream wordpress {
  server    10.0.0.1;
  server    10.0.0.2;
  ...
}

I want to copy the contents of /etc/nginx/upstream.conf in between the ## START UPSTREAM and ## END UPSTREAM blocks. The desired result:

## START UPSTREAM
upstream wordpress {
  server    10.0.0.1;
  server    10.0.0.2;
  ...
}
## END UPSTREAM

http {
...
}

So far, I've tried to use sed (with the help of other StackOverflow solutions):

sed -i '/## START UPSTREAM/,/## END UPSTREAM/ r /etc/nginx/upstream.conf' /etc/nginx/nginx.conf

However, the above code doesn't work--it just fails silently. How can I modify sed to properly replace all text between the placeholders?

Thanks in advance!

Community
  • 1
  • 1
hao_maike
  • 2,929
  • 5
  • 26
  • 31

3 Answers3

1

This might work for you (GNU sed):

sed -i -e '/## START UPSTREAM/,/## END UPSTREAM/{//!d;/## START UPSTREAM/r /etc/nginx/upstream.conf' -e '}' /etc/nginx/nginx.conf
potong
  • 55,640
  • 6
  • 51
  • 83
  • I get an error: sed: -e expression #1, char 1: unknown command: `-' – hao_maike Aug 05 '13 at 21:19
  • Ah, thanks, it's working now. However, every time I run the command it duplicates the block like this: http://pastebin.com/Zvv8eb3e. How can I make sure it also replaces all of the current block contents? – hao_maike Aug 05 '13 at 21:35
  • I'm guessing here. But I think you may need to edit your `nginx.conf` file back into a pristine state then apply above solution. It may be that having run the solution(s) again and again that file is now corrupt. – potong Aug 06 '13 at 07:19
0

Here solution with using hold space ( ... probably not what you looking for):

sed -n '
/\#\# START UPSTREAM/!{H;}; 
/\#\# START UPSTREAM/{p;}; 
${x;p;}' /etc/nginx/upstream.conf /etc/nginx/nginx.conf
Nikolai Popov
  • 5,397
  • 1
  • 17
  • 18
0

I'd use Perl rather than sed for this:

#!/usr/bin/env perl

use strict;
use warnings;

my $config   = '/etc/nginx/nginx.conf';
my $upstream = '/etc/nginx/upstream.conf';

local $/;
open FILE, "<$config" or die $!;   my $cnf=<FILE>; close FILE;
open FILE, "<$upstream" or die $!; my $inc=<FILE>; close FILE;

$cnf =~ s/(## START UPSTREAM)[\s\S]*?(## END UPSTREAM)/$1\n$inc$2/;

open FILE, ">$config" or die $!; print FILE $cnf; close FILE;
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328