문제

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.

도움이 되었습니까?

해결책 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 :)

다른 팁

$ 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top