I have a statistical file populating every minute as below:
2011-11-11-1108 1955 891
2011-11-11-1109 2270 1049
2011-11-11-1110 1930 904
2011-11-11-1111 2030 931
2011-11-11-1112 1944 900
2011-11-11-1113 1922 875
Instead of having the date and time in the given format (2011-11-11-1113) I would like it to view it as a unix timestamp in seconds based on seconds since standard epoch of 1/1/1970. Can anyone assist?:wall:
zaxxon
November 17, 2011, 2:16am
2
You could either cut and multiply it with seconds yourself to get it or use some neat perl function like localtime - perldoc.perl.org
Else try modify the code of the script which is populating the statistical file on minute basis with date timestamp ..
From
$ date +%Y-%m-%d-%H%M
2011-11-17-0720
To
$ nawk 'BEGIN{print srand()}'
1321514472
I have no sufficient right to modify the script generating the statistics.
Can i have a way of appending the output of
$ nawk 'BEGIN{print srand()}'
as a new column of data in the file
try this first to see if it works on yours:
(export TZ=UTC;date -j -f %Y-%m-%d-%H%M%S 1970-01-01-000000)
Thu Jan 1 00:00:00 UTC 1970
(export TZ=UTC;date -j -f %Y-%m-%d-%H%M%S +%s 1970-01-01-000000)
0
export TZ=UTC;date -j -f %Y-%m-%d-%H%M%S +%s 1970-01-01-000000
export: Command not found.
date: illegal option -- j
date: illegal option -- f
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
vbe
November 24, 2011, 10:07am
7
The TZ setting can be quite awkward depending system which you say nothing of... Nor do you mention what shell you use...
Try this...
awk '{match($0,"([0-9-]+)-([0-9]+)",a);cmd="date -d\""a[1]" "a[2]"\" +%s"; cmd|getline x; $1=x}1' input_file
1320989880 1955 891
1320989940 2270 1049
1320990000 1930 904
1320990060 2030 931
1320990120 1944 900
1320990180 1922 875
Hope this is what you want!
--ahamed
Try with perl.
perl -ne 'use POSIX; /^([0-9]+)-([0-9]+)-([0-9]+)-([0-9]{2})([0-9]{2})(.*)/; print mktime(0, $5, $4, $3, $2 - 1, $1 - 1900)."$6\n";' file
Ok it was just a try.
That's why I asked to see if that command worked.
OK we'll need to do some proper stuff,
do you have perl or C?
try:
man strftime
see if it's available.
---------- Post updated at 04:17 PM ---------- Previous update was at 04:11 PM ----------
macmonster much better
awk '{split($1,a,"-");match(a[4],"(..)(..)",b);t=a[1]OFS a[2]OFS a[3]OFS b[1]OFS b[2]" 00";$1=mktime(t)}1'
input_file
--ahamed