Inserting a row if patterns not followed

Hi all,

I have a file of thousands of lines or rows with 2 columns. It is supposed to follow a certain pattern or blocks of 6 rows based on column 1. Below is an example:

a_1 1
a_1 0.9
a_1 12
a_1 3
a_1 0
a_1 1
b_2 1
b_2 0
b_2 3
c_1 1
c_1 0
c_1 3
c_1 2
c_1 6
c_1 2
d_3 3
d_3 4
e_1 3
e_1 3
e_3 4
e_3 56
...
...

What I need is if the pattern does not follow the pattern of 6 rows in each of the blocks, then insert the content of the row in that pattern of block and add zero (0) in the second column.

The expected format I would like to get is:

a_1 1
a_1 0.9
a_1 12
a_1 3
a_1 0
a_1 1
b_2 1
b_2 0
b_2 3
b_2 0
b_2 0
b_2 0
c_1 1
c_1 0
c_1 3
c_1 2
c_1 6
c_1 2
d_3 3
d_3 4
d_3 0
d_3 0
d_3 0
d_3 0
e_1 3
e_1 3
e_1 0
e_1 0
e_1 0
e_1 0
e_3 4
e_3 56
e_3 0
e_3 0
e_3 0
e_3 0

Please help me know how to do this editing in awk or in sed

Try:

awk -v n=6 '
  function fill() {
    while (++i<=n)
      print p,0
    i=0
  } 
  $1!=p && NR>1 {
    fill()
  }
  {
    ++i
    p=$1
    print
  }
  END{
    fill()
  }
' file
1 Like