Add current time stamp column in existing csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add current time stamp column in existing csv file
# 1  
Old 01-08-2018
Wrench 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
Code:
 awk -F "," 'BEGIN{ OFS="," } {$6=system("date '+%Y%m%d%H%M%S'");print}' final_copy.csv

output for above code:
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


Last edited by Don Cragun; 01-08-2018 at 01:30 AM.. Reason: Change HTML tags to CODE tags.
# 2  
Old 01-08-2018
Quote:
Originally Posted by netdbaind
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
Code:
 awk -F "," 'BEGIN{ OFS="," } {$6=system("date '+%Y%m%d%H%M%S'");print}' final_copy.csv

output for above code:
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:
Code:
 awk -F "," -v date=$(date '+%Y%m%d%H%M%S') 'BEGIN{ OFS="," } {$6=date;print}' final_copy.csv

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-08-2018
If you REALLY need each line's timestamp, try
Code:
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

# 4  
Old 01-09-2018
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:

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


Moderator's Comments:
Mod Comment Please use correct CODE (not HTML) tags as required by forum rules!

Last edited by RudiC; 01-09-2018 at 01:05 PM.. Reason: Changed HTML to CODE tags.
# 5  
Old 01-09-2018
Try
Code:
awk -F "," 'BEGIN{ OFS=","; cmd = "date '+%Y%m%d%H%M%S'"} NR>1 {cmd | getline $6; close (cmd)} 1' file

This User Gave Thanks to RudiC For This Post:
# 6  
Old 01-09-2018
Borrowing from Don:
Code:
awk -F, -v date=$(date '+%Y%m%d%H%M%S') 'FNR>1{$6=date}1' OFS=, final_copy.csv

This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Diplay current time stamp in the output

I want to display the command output in a particular format. can you please suggest how can i do this Output of the command Name= XYZ Company= Alpha Department= Accounts Country= Singapore Name=MNC Company= Beta Department= Engineering country=Malta Name=ABC Company=Gamma... (2 Replies)
Discussion started by: swets
2 Replies

2. Shell Programming and Scripting

Change date time stamp of existing file

I have a file hello.txt which was created today (today's date timestamp) I wish to change its date timestamp (access, modified, created) to 1 week old i.e one week from now. uname -a SunOS mymac 5.11 11.2 sun4v sparc sun4v Can you please suggest a easy way to do that ? (12 Replies)
Discussion started by: mohtashims
12 Replies

3. Shell Programming and Scripting

How to add the dat time stamp just before the last extension of a file?

hi, A file name is in the form inside a variable FILE_NAME="s1.txt.xls" i want to append date and time stamp inbetween s1.txt and .xls. so after appending date time stamp the file name would be MOD_FILE_NAME="s1.txt.201307251422.xls" currently i am using the below code... (4 Replies)
Discussion started by: Little
4 Replies

4. Shell Programming and Scripting

How do i find a file with the current time stamp in it??

I want to find a file using find or any utility having the current date time stamp..... I have an alternate way to do that.... but that is too way out of logic... so looking out something with find itself (5 Replies)
Discussion started by: nikhil jain
5 Replies

5. Shell Programming and Scripting

add string and time stamp on each line of file

I have file A.txt A 1023 B 123 C 1223 I want output Hello_12PM_A 1023 Hello_12PM_B 123 Helll_12PM_C 1223 Add Hello and time stamp in AM and PM. (4 Replies)
Discussion started by: asavaliya
4 Replies

6. Ubuntu

How to add a data column in existing file

Hi All I need to add a column on my existing data file. I know similar posts are there but none of them were meeting my requirement. My input is 1.20 3.44 4.88 5.11 4.99 3.22 1.89 3.89 2.90 Desired output 1 1.20 3.44 4.88 2 5.11 4.99 3.22 3 1.89 3.89 2.90 I will... (2 Replies)
Discussion started by: mahbub03
2 Replies

7. Shell Programming and Scripting

How to Unzip to current time stamp?

I need the current time stamp to the unzipped file , any helpful option in unzip command ? Thank you. (1 Reply)
Discussion started by: almanto
1 Replies

8. Shell Programming and Scripting

unziping to current time stamp

Basically when ever unzipping the .zip file , what ever the file exist in the .zip file with the time stamp, that is the same time stamp after unzip. But if i need the current time stamp to the unzipped file(time stamp whenever unzip process occurs ) any helpful option in unzip command ??? ... (4 Replies)
Discussion started by: posix
4 Replies

9. Shell Programming and Scripting

how to add a new column in an existing file

Hi guys, Please help me if u have some solution. I have a file with three columns separated by ':' - INPUT_FILE C416722_2 : calin Dirigent : Dirigent AC4174_6 : Jac : cal_co TC4260_5 : [no : lin kite BC426302_1 : [no : calin Dirigent lin JC426540_3 : lin Pymo_bin : calin TC428_3 : no7... (4 Replies)
Discussion started by: sam_2921
4 Replies

10. UNIX for Dummies Questions & Answers

Inserting Date&Time Stamp In Existing Log File

I am trying to insert a line with a date stamp in a file that is used to monitor activity in one of our directories. By doing this, I want to grep that file each day and go to the last entry for each time a error occurred and pull all errors generated if any exist. If error exists I want that error... (3 Replies)
Discussion started by: shephardfamily
3 Replies
Login or Register to Ask a Question