Filling in the empty columns using the rows above them

I have a text file that looks like the following:

rs529715 CFD
rs1550758 CIDEB
Magmas
rs12542019 CPNE1
RBM12
rs10515181 CPNE1
RBM12
rs4411112 CPT1A

Some rows have 1 column, whereas some have 2. The ones that have 2 columns have one column (1st column) that start with rs.

I need to manipulate this text file so that the rows that have a single column get a second column so that the single column becomes the second column of the text file and the first column gets copied from the first column of the row above. So that the output looks like this:

rs529715 CFD
rs1550758 CIDEB
rs1550758 Magmas
rs12542019 CPNE1
rs12542019 RBM12
rs10515181 CPNE1
rs10514181 RBM12
rs4411112 CPT1A

Thanks!

Hi,

Try this one,

awk 'BEGIN{i=0;}{if ( $2 == "" ){print i,$1;}else{print $0;}i=$1;}' Input_File

Cheers,
Ranga:)

Thanks but the code that you just posted only works to fill in the first missing gap but it doesnt work for the rest. Instead it fills the rest of the gaps with the second column of the row before, not the first column.

Hi,

sample input and sample output??

Cheers,
Ranga:)

awk 'BEGIN{i=0;}{if ( NF == 1 ){print i,$1;}else{print $0;}i=$1;}'

Seems fine to me:

# awk 'BEGIN{i=0;}{if ($2==""){print i,$1;}else{print $0;}i=$1;}' yy.txt
rs529715 CFD
rs1550758 CIDEB
rs1550758 Magmas
rs12542019 CPNE1
rs12542019 RBM12
rs10515181 CPNE1
rs10515181 RBM12
rs4411112 CPT1A

What results are you getting?

Input:

rs529715 CFD
rs1550758 CIDEB
Magmas
Magmas
rs12542019 CPNE1
RBM12
rs10515181 CPNE1
RBM12
rs4411112 CPT1A

awk 'BEGIN{i=0;}{if ( NF == 1 ){print i,$1;}else{print $0;i=$1;}}' xyz.txt
Output:
rs529715 CFD
rs1550758 CIDEB
rs1550758 Magmas
rs1550758 Magmas
rs12542019 CPNE1
rs12542019 RBM12
rs10515181 CPNE1
rs10515181 RBM12
rs4411112 CPT1A