awk text record - prepend first field to all subsequent fields

Hello everyone, I've suddenly gotten very interested in sed and awk (and enjoying it quite a bit too) because of a large conversion project that we're working on. I'm currently stuck with a very inefficient process for processing text blocks. I'm sure someone here should be able to easily point out what I should be doing instead. I think I already know what I should be doing, but just can't figure out how.

My text is currently in this form, although it doesn't have to be:

Bob
is male
is short
 
Mike
is male
is tall
is friendly
 
Ann
is female
 

I need it to be in this form (for example):

Bob is male
Bob is short
Mike is male
Mike is tall
Mike is friendly
Ann is female

I'm currently accomplishing this with a very brutish awk command:

awk -v RS="" -F"\n" '{ print $1 " " $2 ; print $1 " " $3 ; print $1 " " $4 ; etc

However some of my records have up to 40 fields now so this is getting ridiculous.

I'm assuming that I should be using some form of looping to accomplish this? I've been studying this but I'm just have difficulty wrapping my head around that.

Any suggestions?

nawk 'BEGIN{RS=FS=""}{for(i=2;i<=NF;i++) print $1, $i}' myFile

NOTE: Solaris nawk specific.....

1 Like

Thank you so much!

I made a slight modification which accomplished the desired result, but your syntax help on the looping was pure gold to me. Here's what I ultimately did:

 
<previous processing> | awk 'BEGIN{RS="";FS="\n"}{for(i=2;i<=NF;i++) print $1 $i}'