awk date too many open files

Dear Community;

I have a csv file with msb and lsb in $3 and $5 fields which provides the epochtime (factor 65536). Further I need to convert epochtime to readable datetime.
But am getting an error.

File Sample:

5000a,1000,20671,0,16421,0,1,NULL,0
5000b,1000,20974,0,-16284,0,1,NULL,0
5000c,1000,20974,0,-16269,0,1,NULL,0
5000d,1000,20974,0,-16237,0,1,NULL,0
5000e,1000,20974,0,-16215,0,1,NULL,0
5000f,1000,20974,0,-16194,0,1,NULL,0
5000g,1000,20671,0,16421,0,1,NULL,0

Command:

awk -F, 'BEGIN {OFS = FS} {epochtime=($3*65536+$5)} {"date -r "epochtime"" | getline starttime} {$3=starttime; print}' offer_disabled.txt

Error:

awk: date -r 1374535870 makes too many open files
 input record number 33, file offer_disabled.txt
 source line number 1

Other Attempts (ref: similar issues)

awk -F, 'BEGIN {OFS = FS} {epochtime=($3*65536+$5)} {"date -r "epochtime"" | getline starttime; close(epochtime)} {$3=starttime; print}' offer_disabled.txt 

Or 

awk -F, 'BEGIN {OFS = FS} {epochtime=($3*65536+$5)} {"date -r "epochtime"" | getline starttime; close(starttime)} {$3=starttime; print}' offer_disabled.txt 

Or 
awk -F, 'BEGIN {OFS = FS} {epochtime=($3*65536+$5)} {"date -r "epochtime"" | getline starttime; close(epochtime); close(starttime)} {$3=starttime; print}' offer_disabled.txt 

Or 

awk -F, 'BEGIN {OFS = FS} {"date -r "$3*65536+$5"" | getline starttime; close(starttime)}  {$3=starttime; print}' offer_disabled.txt 

But the same error is returned.

If I run line 33 output

date -r 1374535870

on CLI, it works fine.

Please guide on this issue.

########################

Just to add, am running this on OS X

########################

BR//

What's your OS and date version? The -r has different meanings across systems.
Howsoever, try this modification of your code:

awk -F, 'BEGIN {OFS = FS} {cmd = "date -r" $3*65536+$5; cmd | getline starttime; close (cmd); $3=starttime; print}' file
1 Like

Many Thanks Rudi; This worked fine.

Can you please suggest what I was doing wrong?

BR//

You have to close the file or pipe with EXACTLY the name/string you opened it, identical char by char. Closing "epochtime" or "starttime" you missed the "date -r" partial string.

1 Like

If you have GNU awk (gawk) on your system you could try using the strftime() internal which should run much more quickly:

gawk -F, 'BEGIN {OFS = FS} {$3=strftime("%a %b %e %Y %T",$3*65536+$5); print}' offer_disabled.txt

Adjust the format string %a %b %e %Y %T to your requirements:

Output:

5000a,1000,Wed Dec  5 2012 22:37:57,0,16421,0,1,NULL,0
5000b,1000,Tue Jul 23 2013 09:29:40,0,-16284,0,1,NULL,0
5000c,1000,Tue Jul 23 2013 09:29:55,0,-16269,0,1,NULL,0
5000d,1000,Tue Jul 23 2013 09:30:27,0,-16237,0,1,NULL,0
5000e,1000,Tue Jul 23 2013 09:30:49,0,-16215,0,1,NULL,0
5000f,1000,Tue Jul 23 2013 09:31:10,0,-16194,0,1,NULL,0
5000g,1000,Wed Dec  5 2012 22:37:57,0,16421,0,1,NULL,0