How to split lines containing multiple entries using bash scripting
I have a form file:
Heading1 Heading2 A1 A2 B1 B2
Heading3 Heading4 A3 A4 B3 B4 C1 C2
etc.
Each line contains several records belonging to the same headings. What I am trying to do is split these posts while keeping their headers. In the above example, I would like to do 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: Each line has 2 headers and N number of records, each represented by two fields. Thus, the general view of the length of the lines is 2 + 2 * N. So it is always even
+3
source to share
4 answers
Short awk solution:
awk '{ for(i=3;i<=NF;i+=2) print $1,$2,$i,$(i+1) }' file
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)
- iteration over fields, starting from the 3rd (i+=2
- iteration in pairs)
+3
source to share