I have a file of the form:
Heading1 Heading2 A1 A2 B1 B2
Heading3 Heading4 A3 A4 B3 B4 C1 C2
etc
Each lines contains multiple records belonging to the same headers. What I'm trying to do is split these records preserving their headings. In the example above I would like to produce the following:
Heading1 Heading2 A1 A2
Heading1 Heading2 B1 B2
Heading3 Heading4 A3 A4
Heading3 Heading4 B3 B4
Heading3 Heading4 C1 C2
My main problem is that the number of records per line is not constant.
Edit: Every line has 2 headings and N number of records each one of them is represented with 2 fields. So the general form of lines' length is 2+2*N. So it is always even
Short awk solution:
awk '{ for(i=3;i<=NF;i+=2) print $1,$2,$i,$(i+1) }' file
The output:
Heading1 Heading2 A1 A2
Heading1 Heading2 B1 B2
Heading3 Heading4 A3 A4
Heading3 Heading4 B3 B4
Heading3 Heading4 C1 C2
for(i=3;i<=NF;i+=2) - iterating through the fields starting from the 3rd (i+=2 - iterating pairwise)awk '{for(i=3;i<=NF;i+=2)print $1,$2,$i,$(i+1)}' file
NF is number of fields in line and $i you can use field with number i.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With