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


awk '{for(i=3;i<=NF;i+=2)print $1,$2,$i,$(i+1)}' file

      



NF

- the number of fields in a line and $i

you can use the field with the number i.

+1


source


Here's a solution in pure bash:

#!/bin/bash

while read -r; do
    read -r h1 h2 rest <<< "$REPLY"
    while [ -n "$rest" ]; do
        read -r x1 x2 rest <<< "$rest"
        printf '%s %s %s %s\n' "$h1" "$h2" "$x1" "$x2"
    done
done

      

0


source


With GNU sed:

sed -E 's/^(([^ ]* ){2})(([^ ]* ){2})/\1\3\n\1/;P;D;' file

      

0


source







All Articles