Home > Software design >  sed insert after first match
sed insert after first match

Time:11-24

I am working with a .sh file that has the line

sed -i -e "/\[/r ${NEWPERMS}" ${CURRENTPERMS}

NEWPERMS and CURRENTPERMS are both files. This line has until recently inserted the contents of NEWPERMS after the [ in the CURRENTPERMS file. This has worked because until recently the CURRENTPERMS file has only had a single [. Suddenly, a second [ has appeared, and the above line is inserting after both.

I need it to continue only inserting after the FIRST instance of [. I have found dozens of answers online, none of which seem to work. I trued a "0," before the command, like seems to be the common answer, but it causes the file name of NEWPERMS to be inserted instead.

CodePudding user response:

With GNU sed:

sed -i  "
    0,/\[/{
        /\[/r $NEWPERMS
    }" "$CURRENTPERMS"

Note that 0,/regexp/ address range is a GNU extension.

  • Related