Script to combine lines in a file

Greetings,
I have large file with following format

name1 name2
name3 name4
name5 name6
 child7 child8 child9                      <== there is leading blank space
 child10 child11 child12                 <== there is leading blank space
name13 name14
name15 name16
 child17 child18 child19                 <== there is leading blank space
name20 name21
 child22 child23 child24                 <== there is leading blank space
 child25 child26 child27                 <== there is leading blank space
 child28 child29 child30                 <== there is leading blank space

Need a script to convert to the following:

name1 name2
name3 name4
name5 name6 child7 child8 child9
name5 name6 child10 child11 child12
name13 name14
name15 name16 child17 child18 child19
name20 name21 child22 child23 child24
name20 name21 child25 child26 child27
name20 name21 child28 child29 child30

Basically, there is a "leading space" in the "child" line. When ever found, loop through to print with parent "name" to create above output. If no "child" just print the parent "name".

Any help appreciated. Thank you very much.

Any attempts / ideas / thoughts from your side?

With my simple knowledge of using 'awk', I can combine two lines, but unable to script a loop to print the parent line along with each child line found. Potentially there several 'child' lines which needs to be combined with parent line. Thanks.

Here is an approach:-

 
 awk '
        {
                if ( /^ /  && p )
                {
                        print p, $0
                        A[p]
                }
                else if ( ! ( p in A ) && p )
                        print p
        }
        !/^ / {
                p = $0
        }
' file

 

Awesome Yoda.
Yesterday I gave up on AWK and wrote a BASH script this morning. In Bash, with the loops construct, it was taking about 2min to execute for the sample set of 1000 records. But your awk script done executing in blink of an eye.
Thank you very much.

However, upon verification, looks like the last line is not getting printed. Please check it out.

Please recommend a good AWK manual/user guide to get.
Thanks again.

For handling last line, add an END rule:-

awk '
        {
                if ( /^ /  && p )
                {
                        print p, $0
                        A[p]
                }
                else if ( ! ( p in A ) && p )
                        print p
        }
        !/^ / {
                p = $0
        }
        END {
                if ( ! ( p in A ) && p )
                        print p
        }
' file

The GNU Awk User�s Guide

Try also

awk '!/^ / {printf "%s%s", F?ORS:"", TMP0 = $0; F = 1; next} {printf "%s%s" ORS, F?"":TMP0, $0; F = 0}' file
name1 name2
name3 name4
name5 name6 child7 child8 child9
name5 name6 child10 child11 child12
name13 name14
name15 name16 child17 child18 child19
name20 name21 child22 child23 child24
name20 name21 child25 child26 child27
name20 name21 child28 child29 child30