I have a file that contains data header information followed by data entries. Each header will be different and I need to add the information from each header onto each line of the file, until the next header get encountered. For instance:
"Header 1","head1_info"
date1,data1
date1,data2
"Header 2","head2_info"
date3,data5
data4,data6
I want this file to be appended to:
`"Header 1","head1_info"
head1_info,date1,data1
head1_info,date1,data2
"Header 2","head2_info"
head2_info,date3,data5
head2_info,date4,data6`
I've tried assigning grep to variable, but I don't know how to make it change from to the next one, once it encounters "Header 2", I've also been experimenting with sed and awk, but I can't get them to do what I want either. Any guidance would be greatly appreciated.
This might work for you (GNU sed):
On encountering a header line store it in the hold space. For all other lines, append the hold space to the pattern space and substitute the required info using back references.
Assuming any line which contains a double quote indicates a header line:
If there is some other method to determine a header, just change the pattern matched at the start. (This records the value of column 2 from header lines in the variable
a
. The gsub just strips the double quotes.)