0

I have a block content in my ~/.ssh/config. begin with a line '# BEGIN AUTO GENERATED CONTENT' and end with a line '# END AUTO GENERATED CONTENT'

I have a command replacing content between this two lines, with the content reading from a file named 'config'

sed -i -ne '/# BEGIN AUTO GENERATED CONTENT/ {p; r config' -e ':a; n; /# END AUTO GENERATED CONTENT/ {p; b}; ba}; p' ~/.ssh/config

it works well in ubuntu, but it cannot work in my mac which use bsd sed. it gave out this error message:

sed: 1: "/# BEGIN AUTO GENERATED ...": unexpected EOF (pending }'s)

I need to correct this command to work in bsd. not using gsed in mac

4
  • I suggest to edit the question and add an explanation what your sed script is supposed to do and show the (relevant) contents of ~/.ssh/config.
    – Bodo
    May 7, 2019 at 12:39
  • have you considered dropping sed and writing it in something more portable, awk perhaps? May 7, 2019 at 13:08
  • Why do you have two separate scripts -e '...' with unbalanced braces? Can you combine this to one script? Maybe GNU sed somehow combines all to one script while other implementations handle them separately.
    – Bodo
    May 7, 2019 at 13:14
  • You may want to reword the question title - - it is the opposite of what you require. May 7, 2019 at 15:33

1 Answer 1

0

First off, since you are on a Mac, a lone -i option won't work. You need to supply a value to it, in your case an empty one.

Secondly, it's ok to change the p;b pair to p;d in your case since the -n switch is in effect. The branch b command does not gel well with the closing braces in older seds since.

$ sed -i '' -ne '/# BEGIN AUTO GENERATED CONTENT/{' -e 'p;r config' -e ':a;n' -e '/# END AUTO GENERATED CONTENT/{' -e 'p;d' -e '}' -e 'ba' -e '}' -e 'p' ~/.ssh/config

Unless this is a use-and-throw kinda stuff, it's best to rewrite it in a file, and then invoke as:

$ sed -i '' -nf ./code ~/.ssh/config

$ cat ./code
   /# BEGIN AUTO GENERATED CONTENT/{
      p
      r config
      :loop
         n
         /# END AUTO GENERATED CONTENT/{
            p
            d
         }
      bloop
   }
   p
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .