Join in a single line variable number of lines

Hi all,

I have a file with little blocks beginning with a number 761XXXXXX, and 0, 1, 2 or 3 lines below of it beginning with STUS as follow:

761625820
STUS ACTIVE 16778294
STUS NOT ACTIVE
761157389
STUS ACTIVE 16778294
761554921
STUS ACTIVE 16778294
STUS NOT ACTIVE
STUS ACTIVE OP
761229934
761335544
STUS ACTIVE 16778294

I was trying to join the blocks in a single line as follow desired output:

761625820 STUS ACTIVE 16778294 STUS NOT ACTIVE
761157389 STUS ACTIVE 16778294 
761554921 STUS ACTIVE 16778294 STUS NOT ACTIVE STUS ACTIVE OP
761229934
761335544 STUS ACTIVE 16778294

I tried with something I thinked, but doesn`t seem to work.

awk '{while ($0 ~ /^761/) {getline temp; $0 = $0 temp} print $0}' inputfile

Maybe somebody could help me to get the desired output.

Thanks in advance,

Best regards.

awk '/^761/ {
   if ( NR > 1 ) print line
   line = $0
   next
 }
 { line = line " " $0 }
' "$file"

Many thanks Mr. cfajohnson. Very appreciated your help. It works exactly like I was looking for.

One more thing.

May you or somebody please explain a little bit how your code work?

Thanks again.

sed -n '/^761/{
1{h;}
1!{x;s/\n//g;p;}
}
/^761/!{
${H;x;s/\n//g;p;}
$!{H;}
}'

I have the same pb : joining 3 lines in a data file when they belong to a same sequence number :

like that :

Your suggestion seems being exactly what I try to have (for 3 hours now, I am a very beginner wth sed.. :o)
but could you please detail the explanation ?
instead of trying to match lines not starting with "761" I want lines not starting with a sequence number between parenthesis so I replaced the

sed -n '/^761/{

with

sed -n '/^\([0-9]*\)/{

but I dont succeed in understanding the rest of the code

Thanks for your help !