We have the following scenario that we inherited.
A file with 'hosts per service' is piped into sed.
Based on the service name the list of hosts would be returned.
Command used:
/bin/sh -c 'cat file.txt | sed -n "/main-service]/,/\\[/{/\\[/!p;}"'
OS(s): Ubuntu 14 16 18 20
On the road we found out that sed is returning more hosts that what we expect.
I'm trying to fix this 'greedy' behavior without modifying the 1st part of the sed ( '/main-service]/' ),
A fix for this can be: '/\[main-service]/', but I do not know exactly what it could brake ( this sed is used in a lot of places ), so I'm trying to avoid this edit.
File example ( Sed input ):
[main-service]
hosta
hostb
hostc
[that-main-service]
hostd
hoste
[other-main-service]
hostf
hostg
In the below example we are looking for all the hosts under the 'main-service'
/bin/sh -c 'cat file.txt | sed -n "/main-service]/,/\\[/{/\\[/!p;}"'
However, the output is not the one that we expect
hosta
hostb
hostc
hostf
hostg
I do not know what the creator meant with the sed syntax exactly, but what I'm trying to achieve is:
if service name == '/main-service]/' #find only the line that matches
take all the text until '\n\['
Example here:
https://sed.js.org/?gist=0a90e4d015d02b820072e6cf837e6204
Where the expected output is:
hosta
hostb
hostc
Any help would be appreciated.
Thank you.