Using external "date" command from awk

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using external "date" command from awk
# 1  
Old 06-22-2011
Using external "date" command from awk

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:

Tandy400 $ echo 1308607169 | awk '{print strftime("%c",$1)}'
Mon 20 Jun 2011 03:59:29 PM MDT

That mostly works, but it's in MDT. I need UTC. So, I read the following:

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:

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:

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...

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.

Many thanks in advance.
# 2  
Old 06-22-2011
For the scope of the awk command, unset TZ and it will return zulu (GMT).

echo 1308784748 | TZ= awk '{print strftime("%c",$1)}'
Wed Jun 22 23:19:08 2011

echo 1308784748 | awk '{print strftime("%c",$1)}'    
Wed Jun 22 19:19:08 2011

And this should give you the ability to use the date command if you'd rather:

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
# 3  
Old 06-23-2011
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.
# 4  
Old 06-24-2011
Yes, you could have set it to generate a time for any timezone using something like this:
TZ=/usr/share/zoneinfo/America/Denver date

You can also set multiple environment variables too:
FOO=Hello BAR=world  some_command

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk variable into shell command "date -d": possible...?

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)
Discussion started by: fbird3
3 Replies

2. Shell Programming and Scripting

awk "date" and "system" command

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)
Discussion started by: phaethon
2 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

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)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

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)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

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)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Can someone help me to convert the date format after get it from the "LAST REBOOT" command.

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)
Discussion started by: pichitw
3 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. AIX

xx=`date +"%a %b %d"`;rsh xxx grep "^$XX" zzz ?

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)
Discussion started by: Browser_ice
2 Replies

9. UNIX for Advanced & Expert Users

add seconds to: date"|"time"|"HHMMSS

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)
Discussion started by: anaconga
3 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep ""|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep ""|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)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question