2

I have file A : a nginx config. Somewhere in this file I have {here}

I want to replace that {here} by the content of another file B:

location /blah {
     proxy_pass "https://blah.com";
}

I have tried with sed and awk (gsub) but I have issues with the return/unescaped character.

I found a solution but only works only with pattern, doesn't work with substitution. In this example it will append fileB after my marker instead of replacing it :

sed -i '/{here}/{r fileB
:a;n;ba}' nginx.conf
Bastien974
  • 311
  • 3
  • 13
  • 1
    possible duplicate of [Insert text into file after a specific text](http://stackoverflow.com/questions/25556211/insert-text-into-file-after-a-specific-text). I just answered this question and it's very similar. The awk technique in particular should work. – Tom Fenech Aug 28 '14 at 20:05
  • Actually, it's a little bit different, I've posted an answer anyway. – Tom Fenech Aug 28 '14 at 20:17

3 Answers3

6

This should do it:

awk 'NR==FNR { a[n++]=$0; next } 
/{here}/ { for (i=0;i<n;++i) print a[i]; next }
1' fileB fileA

Build an array containing all of the lines of the file containing the replacement. When the pattern is matched in the second file, print out all of the lines. Otherwise, the 1 at the end means that the original lines are printed.

As Ed has rightly pointed out, the loop initialiser was wrong. I've changed it to i=0 so the first line is printed. Also, this replaces the whole contents of the line containing {here}, which may or may not be what you need.

To overwrite the original file, you can do:

awk '...' fileB fileA > tmp && mv tmp fileA
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • Thanks for this awk solution ! I'd be very interested to see a sed alternative to what I posted too. – Bastien974 Aug 28 '14 at 20:20
  • No problem. Perhaps someone with better sed skills than me will be able to provide a sed solution. – Tom Fenech Aug 28 '14 at 20:23
  • That needs to be `++n` instead of `n++` so the array starts at 1 instead of 0 and then the loop should end at `<=n`. Right now it will skip printing the first line of fileB. This doesn't replace `{here}` with the contents of fileB, it replaces the whole line containing `{here}`. – Ed Morton Aug 29 '14 at 02:45
  • 1
    @Ed thanks, I meant to start the loop at 0. I know that it's typical for functions such as `split` etc. to use 1-based array indices, is there anything wrong with starting at 0? I made the assumption that `{here}` was on its own line. +1 for your solution anyway. – Tom Fenech Aug 29 '14 at 07:08
  • There's no technical problem with starting at zero, it just often leads to bugs like you had where you later forget to "special case" that array when looping through it's members and start it at 0 instead of 1 as usual. It also makes the code a bit harder for others to read as when we see an array starting at zero we have to think a bit harder about why that should be, what it's doing, is it correct to do that, etc. instead of just understanding the code at a glance. I've found it best to ALWAYS start awk arrays at 1 unless you need to use array[0] for some adjuvant purpose. – Ed Morton Aug 29 '14 at 12:00
  • 1
    @Ed Thanks for your insight. As an aside, it's interesting that the postfix increment is sufficient to produce the value of 0, i.e. `awk 'BEGIN{print n++}'` results in 0, rather than an empty string. – Tom Fenech Aug 29 '14 at 12:17
  • 1
    True. Glad it's smart enough to figure out the variable is a number from the context rather than having to actually increment it first. By `adjuvant purpose` by the way, I mean things like where you need to write a function to populate an array from a string (like `split()` does) and it can be useful to populate `array[0]` with the original string or the length of the array or some other meta-information separate from the main purpose/contents of the array. – Ed Morton Aug 29 '14 at 12:36
4

All you need is:

awk 'NR==FNR{rep=(NR>1?rep RS:"") $0; next} {gsub(/{here}/,rep)}1' fileB nginx.conf

assuming fileB doesn't contain &s as they'd be interpreted by the replacement operation as the RE-matching string. If it does then use index() and substr() in a loop.

If {here} is the ONLY thing on the line, tell us about that as it makes the problem a bit simpler.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

This might work for you (GNU sed):

sed 's/\(.*\){here}\(.*\)/echo '\''\1'\''$(cat fileB)'\''\2'\''/e' fileA
potong
  • 55,640
  • 6
  • 51
  • 83
  • 1
    I've tried it, it doesn't keep the return character in fileB. If I understand correctly, you are capturing what's before and after the pattern, and echoing them around fileB. I'm a bit lost in all those single un/escaped quote though. – Bastien974 Aug 29 '14 at 12:46