Shell – Replace between patterns that start and end in different lines

awksedshelltext processing

I have a file with the following format:

Whatever1
Whatever2
Whatever3
binaries=(
text1
bin2
ohhh3
)
Whatever4
Whatever5
Whatever6

I need to replace whatever inside lines binaries=( and ) with a command output.

I'm able to pick the following block with a command like sed -n -e '/binaries=(/,/)/p' filename:

binaries=(
text1
bin2
ohhh3
)

Or even better with this awk '/binaries/,/)/{if(!/binaries|)/)print}' filename:

text1
bin2
ohhh3

But, I have to do it rewriting the original file, and I don't know how to continue.

Best Answer

With GNU sed:

sed '/binaries=(/,/)/{
       //!d
       /)/e uname
     }'

Replace uname with your command.

Related Question