Copy header values into records

I'm using a shell script to manipulate a data file. I have a large file with two sets of data samples (tracking memory consumption) taken over a long period of time, so I have many samples. The problem is that all the data is in the same file so that each sample contains two sets of data. Furthermore, the header line of each data set helpfully indicates the number of fields I should have, but some of the data labels in each record contain sub-labels that increase number of fields in each record. The main data I'm interested in are the labels and the value under AllocBytes in the first data set, and Bytes under the second data set. I think I can reduce the complexity of this problem of getting my fields sorted out by carrying a value from the header down to each record so I can tell each data set apart. Then I can know where my field of interest is by counting the fields from right to left. I'm hoping I can use the same method to copy the time stamp into each record as well.

The file looks something like this:

Sep 13 00:00:00
Name,Size,AllocSize,AllocBlocks,AllocBytes,MaxAllocBlocks,MaxAllocBytes
label1,4,8,4727,37816,4727,37816
label2,sublabelA,8,12,9037,108444,9062,108744
label3,sublabelA,sublabelB,8,12,1,12,2,24
label4,sublabelA,sublabelB,sublabelC,2,8,12,0,0,2,24
label5,8,12,2,24,2,24
Name,Allocs,Bytes,MaxAllocs,MaxBytes,FuncCalls
label101,1,8,1,8,1
label102_sublabelA,sublabelB,0,0,1,8,6
label103,sublabelA,sublabelB,sublabelC,0,0,1,8,27
label104,6,2,32,2,32,2
label105,sublabelA,sublabelB,0,0,2,16,24
Sep 13 00:00:10
Name,Size,AllocSize,AllocBlocks,AllocBytes,MaxAllocBlocks,MaxAllocBytes
label1,4,8,4727,37816,4727,37816
label2,sublabelA,8,12,9037,108444,9062,108744
label3,sublabelA,sublabelB,8,12,1,12,2,24
label4,sublabelA,sublabelB,sublabelC,2,8,12,0,0,2,24
label5,8,12,2,24,2,24
Name,Allocs,Bytes,MaxAllocs,MaxBytes,FuncCalls
label101,1,8,1,8,1
label102_sublabelA,sublabelB,0,0,1,8,6
label103,sublabelA,sublabelB,sublabelC,0,0,1,8,27
label104,6,2,32,2,32,2
label105,sublabelA,sublabelB,0,0,2,16,24

Here's what I would like to do:

Sep 13 00:00:00
Name,Size,AllocSize,AllocBlocks,AllocBytes,MaxAllocBlocks,MaxAllocBytes
Sep 13 00:00:00,label1,4,8,4727,37816,4727,37816,MaxAllocBytes
Sep 13 00:00:00,label2,sublabelA,8,12,9037,108444,9062,108744,MaxAllocBytes
Sep 13 00:00:00,label3,sublabelA,sublabelB,8,12,1,12,2,24,MaxAllocBytes
Sep 13 00:00:00,label4,sublabelA,sublabelB,sublabelC,2,8,12,0,0,2,24,MaxAllocBytes
Sep 13 00:00:00,label5,8,12,2,24,2,24,MaxAllocBytes
Name,Allocs,Bytes,MaxAllocs,MaxBytes,FuncCalls
Sep 13 00:00:00,label101,1,8,1,8,1,FuncCalls
Sep 13 00:00:00,label102_sublabelA,sublabelB,0,0,1,8,6,FuncCalls
Sep 13 00:00:00,label103,sublabelA,sublabelB,sublabelC,0,0,1,8,27,FuncCalls
Sep 13 00:00:00,label104,6,2,32,2,32,2,FuncCalls
Sep 13 00:00:00,label105,sublabelA,sublabelB,0,0,2,16,24,FuncCalls
Sep 13 00:00:10
Name,Size,AllocSize,AllocBlocks,AllocBytes,MaxAllocBlocks,MaxAllocBytes
Sep 13 00:00:10,label1,4,8,4727,37816,4727,37816,MaxAllocBytes
Sep 13 00:00:10,label2,sublabelA,8,12,9037,108444,9062,108744,MaxAllocBytes
Sep 13 00:00:10,label3,sublabelA,sublabelB,8,12,1,12,2,24,MaxAllocBytes
Sep 13 00:00:10,label4,sublabelA,sublabelB,sublabelC,2,8,12,0,0,2,24,MaxAllocBytes
Sep 13 00:00:10,label5,8,12,2,24,2,24,MaxAllocBytes
Name,Allocs,Bytes,MaxAllocs,MaxBytes,FuncCalls
Sep 13 00:00:10,label101,1,8,1,8,1,FuncCalls
Sep 13 00:00:10,label102_sublabelA,sublabelB,0,0,1,8,6,FuncCalls
Sep 13 00:00:10,label103,sublabelA,sublabelB,sublabelC,0,0,1,8,27,FuncCalls
Sep 13 00:00:10,label104,6,2,32,2,32,2,FuncCalls
Sep 13 00:00:10,label105,sublabelA,sublabelB,0,0,2,16,24,FuncCalls

Thank you

awk -F"," 'NR==1 { D=$0 ; print ; next }
$1 == "Name" { print ; next }
{ $0=D"$0" ; print }' inputfile

Two minor corrections to Corona688's proposal:

awk -F"," 'NF==1        { D=$0 ; print ; next }
           $1 == "Name" { print ; next }
                        { $0=D","$0 ; print }
        ' file
1 Like