Sign in with
Sign up | Sign in
Your question

Insert date and time into data file

Last response: in Linux/Free BSD
Share
December 22, 2010 5:58:21 PM

Hello,
I have a data file which looks like this:
00200|2010-11-16 21:00:14
ITRANS.CMS|H
I want to replace line 1, starting in column 7 with date, in the format of ccyy-mm-dd followed by a space, then time in format of hh:mm:ss

Any help would be great
Jim
December 23, 2010 4:02:14 PM

Isn't that what you already have? And a bit more detail on the problem could be helpful - is it just one little file which you want to change with search/replace, or something which is produced by a script every day in quantity, etc?

I'd write a little python script to modify the file - you might eventually want to change something else than just the date so that could be handy then. I think python had separate date and time modules which you can use to parse the file and conveniently switch around formats and so on.
December 24, 2010 7:27:33 PM

jim merchant said:
Hello,
I have a data file which looks like this:
00200|2010-11-16 21:00:14
ITRANS.CMS|H
I want to replace line 1, starting in column 7 with date, in the format of ccyy-mm-dd followed by a space, then time in format of hh:mm:ss

Any help would be great
Jim


  1. hbarta@bonsai:~$ echo '00200|2010-11-16 21:00:14 ITRANS.CMS|H'|sed s/....-..-..\ ..:..:../`date +"%Y:%M:%d %H:%m:%S"|awk '{print $1 $2}' `/
  2. 00200|2010:20:2415:12:42 ITRANS.CMS|H
(modulo line numbers...)

This should give you some idea how to do it using a regular expression (which isn't too specific) and the date command. Awk is used only to strip the training newloine from the date command output and the back ticks substitute the output of the date/awk command into the surrounding command. Of course you'd cat your file instead of echoing the line.


Edit: Dang - I can't seem to figure out how to preserve the space between date and time. :( 

HTH,
hank
!