awk

AWK copy value from $1 to line below


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

Solution

  • 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