awk

AWK print the value from column $3 in column $4 based on the last duplicate from column $1


I have an input file.

30.01.2025                  47288.39        47288.39 
30.01.2025                  -1.00           47287.39 
31.01.2025                  50.00           47337.39 
31.01.2025                  -1600.00        45737.39 
31.01.2025                  1880.00         47617.39 
31.01.2025                  18.21           47635.60 
31.01.2025                  -3.46           47632.14 
03.02.2025                  -51.00          47581.14 
03.02.2025                  -10.00          47571.14 
04.02.2025                  -1600.00        45971.14 
04.02.2025                  1609.00         47580.14 
05.02.2025                  -800.00         46780.14 
05.02.2025                  -320.00         46460.14 
05.02.2025                  51.00           46511.14 
05.02.2025                  800.00          47311.14 
05.02.2025                  11.00           47322.14 

enter image description here

I want to get an output file.

30.01.2025     47288.39      47288.39 
30.01.2025     -1.00         47287.39      47287.39
31.01.2025     50.00         47337.39 
31.01.2025     -1600.00      45737.39 
31.01.2025     1880.00       47617.39 
31.01.2025     18.21         47635.60 
31.01.2025     -3.46         47632.14      47632.14
03.02.2025     -51.00        47581.14 
03.02.2025     -10.00        47571.14      47571.14 
04.02.2025     -1600.00      45971.14 
04.02.2025     1609.00       47580.14      47580.14
05.02.2025     -800.00       46780.14 
05.02.2025     -320.00       46460.14 
05.02.2025     51.00         46511.14 
05.02.2025     800.00        47311.14 
05.02.2025     11.00         47322.14      47322.14

I tried.

awk '{if(!seen[$1]++){print $0}}' File_1

Solution

  • You want to duplicate $3 when the current record is that last one for that day.

    I find it easier to reverse the lines, do something for the first "whatever", and then re-reverse the lines:

    tac File_1 | awk '$1 != prev {prev = $1; $0 = $0 FS $3} 1' | tac
    

    Ordinarily I would write $4 = $3 but that will destroy the spacing for the updated lines.