I have a file named file, whose content is
noise
noise
X noise STUFF1 noise STUFF2 noise
noise
Y noise STUFF3 noise
noise
and I assert that X and Y are distinct, that each occur once in file, and that X occurs first.
I'm able to issue a sed command to extract the first pieces of information, the like of
$ sed -n '/X/s/\(.*\)\(…\)\(.*\)\(…\)/\2 \4/p' < file
STUFF1 STUFF2
$
and a similar one to extract STUFF3 (¹), but what I'd really like to do is to find the right sed incantation so that
$ sed … < file
STUFF3 STUFF1 STUFF2
$
(and possibly learn, at last! how sed's hold buffer works).
(1) This is not a question on regular expression, I know how to insulate the pieces of text that I need. I need to save the info I've collected and output it at the right time.