Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split lines that contain multiple records using bash scripting

Tags:

bash

sed

awk

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

like image 296
Mewtwo Avatar asked Feb 15 '26 04:02

Mewtwo


2 Answers

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)
like image 135
RomanPerekhrest Avatar answered Feb 16 '26 18:02

RomanPerekhrest


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.

like image 44
tso Avatar answered Feb 16 '26 16:02

tso



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!