Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWK copy value from $1 to line below

Tags:

awk

Input file.

44

55



14
15

16

I want to get an output file.

44
44
55
55
55
55
14
15
15
16
16 

I tried.

awk '{print $NF-1}' plik 
43
-1
54
-1
-1
-1
13
14
-1
15
like image 675
Tedee12345 Avatar asked Sep 02 '25 15:09

Tedee12345


1 Answers

What I would do:

awk 'NF{last=$0;print;next} {print last}' file

or

awk 'NF{last=$0;print;next} {$0=last}1' file

That yields:

44
44
55
55
55
55
14
15
15
16
16

In pseudo code, that means:

if NF > 0; then
    last=$0
    print $0
else
    print last
like image 84
Gilles Quenot Avatar answered Sep 04 '25 05:09

Gilles Quenot