Question

What is the best way to go about printing the above field if an entry is blank?

E.g:

0.01 0:00 2014-06-01
0.04 0:20 
0.08 0:40 
0.14 1:00 
0.21 1:20 
0.04 0:00 2014-06-02
0.04 0:20 
0.06 0:40

To become:

0.01 0:00 2014-06-01
0.04 0:20 2014-06-01
0.08 0:40 2014-06-01
0.14 1:00 2014-06-01
0.21 1:20 2014-06-01
0.04 0:00 2014-06-02
0.04 0:20 2014-06-02
0.06 0:40 2014-06-02

I've tried with awk, but having no luck.

Was it helpful?

Solution 3

With awk:

NF == 3 { date=$3; print; }
NF == 2 { $3=date; print; }

My previous attempt inserted an extra space. And @EdMorton's variant is cooler :)

OTHER TIPS

$ awk '{$3=($3?$3:p);p=$3}1' file
0.01 0:00 2014-06-01
0.04 0:20 2014-06-01
0.08 0:40 2014-06-01
0.14 1:00 2014-06-01
0.21 1:20 2014-06-01
0.04 0:00 2014-06-02
0.04 0:20 2014-06-02
0.06 0:40 2014-06-02
awk '
 NF==3 {t=$3}
 NF==2 {$3=t}
 1'  file
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top