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?
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];
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$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With