Jeffrey Van Laethem
Jeffrey Van Laethem

Reputation: 2651

sed insert after first match

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.

Upvotes: 0

Views: 73

Answers (1)

M. Nejat Aydin
M. Nejat Aydin

Reputation: 10123

With GNU sed:

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

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

Upvotes: 1

Related Questions