Add current time stamp column in existing csv file

Hi ,

I want to add a new column 'current_time stamp' in my existing csv file with current time stamp for all the records.I tried something this but this is printing 0 with date & time and printed date one line above header.Please help

 awk -F "," 'BEGIN{ OFS="," } {$6=system("date '+%Y%m%d%H%M%S'");print}' final_copy.csv

output for above code:

 20180107231531
Hostname,IP-Address,Instance-status,Region,RHEL-version,0
20180107231531
local_host_001,110.22.66.100,running,AMER,7.2.3,0
20180107231531

The awk system() function returns the exit code of the command that it executes; not the output produced by that command. Unless your input is coming from a slow running pipeline (which is not the case in your example) and you need to get the actual timestamp at the time that particular line was processed by awk , I would suggest just using:

 awk -F "," -v date=$(date '+%Y%m%d%H%M%S') 'BEGIN{ OFS="," } {$6=date;print}' final_copy.csv
1 Like

If you REALLY need each line's timestamp, try

awk -F "," 'BEGIN{ OFS=","; cmd = "date '+%Y%m%d%H%M%S'"} {cmd | getline $6; close (cmd); print}' file
Hostname,IP-Address,Instance-status,Region,RHEL-version,20180108093456
local_host_001,110.22.66.100,running,AMER,7.2.3,20180108093456

thanks for the reply but both the script is printing time stamp value in header column, how I can skip the first line.Please help.

here is what it printing now:

Hostname,IP-Address,Instance-status,Region,RHEL-version,20180109115222

Try

awk -F "," 'BEGIN{ OFS=","; cmd = "date '+%Y%m%d%H%M%S'"} NR>1 {cmd | getline $6; close (cmd)} 1' file 
1 Like

Borrowing from Don:

awk -F, -v date=$(date '+%Y%m%d%H%M%S') 'FNR>1{$6=date}1' OFS=, final_copy.csv
1 Like