awk

AWK add line number to date


I have an input file.

A
B
C

I use a command that gives the result.

awk -v date="$(date +%d-%m-%Y --date="$NR day")" '{print $0, date}' File_1

A 16-02-2025
B 16-02-2025
C 16-02-2025

I want to get the result.

A 16-02-2025
B 17-02-2025
C 18-02-2025

Solution

  • What I would do:

    $ awk '{printf "%s ", $0; system("date +%d-%m-%Y -d "NR"\\ day")}' File_1
    A 16-02-2025
    B 17-02-2025
    C 18-02-2025
    

    When you do:

    -v date="$(date +%d-%m-%Y --date="$NR day")"
    

    the variable is assigned only one time.