For some dumb reason, I'm having the strangest hard time getting awk to convert epoch time to UTC human time. I'm using CentOS 5.5 and gawk 3.1.5. There are 2 approaches that I know. First, the strftime call:
That mostly works, but it's in MDT. I need UTC. So, I read the following:
Quote:
strftime([format [, timestamp [, utc-flag]]])
This function returns a string. It is similar to the function of the same name in ISO C. The time specified by timestamp is used to produce a string, based on the contents of the format string. If utc-flag is present and is either non-zero or non-null, the value is formatted as UTC (Coordinated Universal Time, formerly GMT or Greenwich Mean Time). Otherwise, the value is formatted for the local time zone. The timestamp is in the same format as the value returned by the systime function. If no timestamp argument is supplied, gawk uses the current time of day as the timestamp. If no format argument is supplied, strftime uses "%a %b %d %H:%M:%S %Z %Y". This format string produces output that is (almost) equivalent to that of the date utility. (Versions of gawk prior to 3.0 require the format argument.)
Nifty, right? Ok, so I do, and get back, this:
Code:
Tandy400 $ echo 1308607169 | awk '{print strftime("%c",$1,0)}'
awk: fatal: 3 is invalid as number of arguments for strftime
So, apparently the C function that awk is calling can't support that third option. On to using the shell "date" command. I can do this:
Code:
Tandy400 $ date -ud @1308607169
Mon Jun 20 21:59:29 UTC 2011
That's what I want; however, getting awk to work with that is not working. So, how might I get this to work? I need awk to somehow run that command on a field and save its output to a variable that awk can work with later. Something like...
Code:
echo 1308607169 | awk '{htime=("date -ud @"$1 | getline) ; print "Epoch time converts to " htime}'
...except that I'd prefer that it work. system() seems right out-- I need the actual response, not the status code. Any thoughts?
Performance is not a big deal... this conversion will work with a handful of dates each day. For my own education, though, I'd love to understand how to get both working, as it seems like there really should be a way.
And this should give you the ability to use the date command if you'd rather:
Code:
awk 'BEGIN { c="date -ud @1308607169"; c|getline; close( c ); print $0; }';
Mon Jun 20 21:59:29 UTC 2011
I usually write a cmd() function that accepts the command, executes, closes the pipe, and returns the output. Very useful for single line output, a bit more tricky to snarf multiple output, but not difficult.
Last edited by agama; 06-22-2011 at 09:30 PM..
Reason: Added second example
Ooh, that's cool. I've never seen the bit with unsetting (and, presumably, setting) a system variable only for the scope of a command. Many thanks for great reply.
Hello, there!
I am trying to pass an awk variable into a shell command in order to collect the result into an awk variable; in Bash it does work, as in:
v='2'; date -d "now + $v weeks"
But in awk it does not, as in:
v="2"
"date -d 'now + v weeks'" | getline newdate
close ("date -d 'now... (3 Replies)
Hello experts!
I need your help please
I have a file.txt of which I want to extract 3rd and 4th columns with date with the form e.g.:
2016-11-25 03:14:50and pass them to "date" command, but also append the 9th column in a file as well.
So I want to execute
date -d '2016-11-25 03:14:50' ... (2 Replies)
How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address
and column 3 contains “cc” e-mail address to include with same email.
Sample input file, email.txt
Below is an sample code where... (2 Replies)
logs:
"/home/abc/public_html/index.php"
"/home/abc/public_html/index.php"
"/home/xyz/public_html/index.php"
"/home/xyz/public_html/index.php"
"/home/xyz/public_html/index.php"
how to use "cut" or "awk" or "sed" to get the following result:
abc
abc
xyz
xyz
xyz (8 Replies)
Hi,
I have line in input file as below:
3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL
My expected output for line in the file must be :
"1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL"
Can someone... (7 Replies)
Can someone help me to convert the date format after get it from the "LAST REBOOT" command.
these is the standard output.
bash-3.00# last reboot
reboot system boot Fri Aug 6 15:07
reboot system down Fri Aug 6 15:04
reboot system boot ... (3 Replies)
AIX 4.2
I am trying to do an rsh grep to search for date records inside server logs by doing this :
xx=`date +"%a %b %d"`
rsh xxx grep "^$XX" zzz
gives :
grep: 0652-033 Cannot open Jun.
grep: 0652-033 Cannot open 11.
But if I do :
xx=`date +"%a %b %d"`
grep "^$XX" zzz
it works... (2 Replies)
Hey all,
I have a shell that invokes a AWK.
In this AWK i want invoke a function that receives 3 parameters:
date: 20080831
time: 235901
duration: 00023
that function receive this 3 parameters and sum to this value two more seconds:
2008083123590100025
Remember that in case that... (3 Replies)
Hi Friends,
Can any of you explain me about the below line of code?
mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`
Im not able to understand, what exactly it is doing :confused:
Any help would be useful for me.
Lokesha (4 Replies)