Sed Expression – Extract Group of First Match with Sed

sed

I have the following text:

somegarbageSTARTfirstgroupENDsomeendgarbage
someohtergarbageSTARTsecondgroupENDsomeotherendgarbage
...

I would like to extract firstgroup using sed.

I tried sed -nr 's/.*START(.*)END.*/\1/p', but it doesn't work, it extracts all matches not only the first one.

Best Answer

sed -nr '/.*START(.*)END.*/{s/.*START(.*)END.*/\1/p;q}'
# or (probably faster)
sed -nr 's/.*START(.*)END.*/\1/p; t quit; b end; : quit; q; : end;'
Related Question