Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling empty fields

I have a very large tab-delimited file (approx 12 million lines) that looks like this:

F1    1
      2
      700
F2    89
      900
      10000
      19
F3    100
      60001

Is there any way I can make this like:

F1    1
F1    2
F1    700
F2    89
F2    900
F2    10000
F2    19
F3    100
F3    60001

I've tried using sed scripts but it takes so long.

For example

sed 's/^/F1/' FILE | cut -c3- > FILE1 ; mv FILE1 FILE

I could do it in excel using

=IF(a2=="",c1,a2)

and dragging down. However Excel will only allow me to load a certain amount of lines.

(Assuming I've copied "F1" to C1)

Surely there's an easier way with awk or sed?

like image 816
Oddish Avatar asked Dec 05 '16 20:12

Oddish


2 Answers

perl -F'\t' -lane'$h = $F[0] ||= $h; print join "\t", @F'

Assignments are right-associative, so

$h = $F[0] ||= $h;

is equivalent to

$h = ( $F[0] ||= $h );

and thus to

$F[0] ||= $h;
$h = $F[0];

and

$F[0] = $h if !$F[0];
$h = $F[0];
like image 99
ikegami Avatar answered Sep 16 '22 11:09

ikegami


awk to the rescue!

$ awk 'BEGIN {FS=OFS="\t"} 
             {if($1!="") p=$1; else $1=p}1' file

F1      1
F1      2
F1      700
F2      89
F2      900
F2      10000
F2      19
F3      100
F3      60001

this is the input file I used

$ cat -A file

F1^I1$
^I2$
^I700$
F2^I89$
^I900$
^I10000$
^I19$
F3^I100$
^I60001$
like image 28
karakfa Avatar answered Sep 18 '22 11:09

karakfa