sed replace?

Hello. I have this file:

XXXX
1 AAAA
2 BBBB
3 CCCC
4 DDDD
YYYY
1 AAAA
2 BBBB
3 CCCC
4 DDDD

Desired output:

XXXX AAAA
XXXX BBBB
XXXX CCCC
XXXX DDDD
YYYY AAAA
YYYY BBBB
YYYY CCCC
YYYY DDDD

Any help would be appreciated.

Does it have to be sed?

awk 'NF==1{x=$1}NF==2{$1=x;print}' file
2 Likes

no it doesn't need to be sed. I'm not getting any output.

What operating system are you using? Do you have more fields in your file?

1 Like

Yes it's working, there were a 2nd set of fields in the file! Thank you so much. Cheers!

With sed:
sed -n '/^[^1-4]/h;/^[1-4]/{G;s/^[1-4]//;s/\(.*\)\n\(.*\)/\2\1/;p}' <file
--
Bye