Carol Carol - 4 months ago 8
Linux Question

Filling the empty rows with the first row

I have tab delim file like follows:

a 12 17 bac 30 42
a 15 18
a 18 22


I would like to repeat the the column bac 30 42 to remaining rows like follows:

a 12 17 bac 30 42
a 15 18 bac 30 42
a 18 22 bac 30 42


I tried following command
awk '{$4=$4; $5=$5;$6=$6; print}' file.txt
but it didnt do any modification to the file. how can I achieve this in awk.

Answer
$ awk -F"\t" -v OFS="\t" '$4!="" {four=$4; five=$5; six=$6} $0!="" {$4=four; $5=five; $6=six; print $0}' test.in
a 12 17 bac 30 42
a 15 18 bac 30 42
a 18 22 bac 30 42
Comments