1752802 Members
4932 Online
108789 Solutions
New Discussion юеВ

AWK confusion

 
SOLVED
Go to solution
Patrick Wallek
Honored Contributor

AWK confusion

I am trying to do some mass conversions of epoch time, to m/d/y hh:mm:ss formatted time. I have acquired an awk script from the internet that converts epoch time (epoch_time.awk is attached to this thread).

The script works well when I run it from the command line and pass the epoch time string.

# ./epoch_time.awk 1168097143
20070106152543

My problem is that this is not working when I am trying to use it from within a for loop or while loop. Such as:

# for i in $(cat myfile)
do
./epoch_time.awk ${i}
done
19700101000000
19700101000000
19700101000000
19700101000000
19700101000000
19700101000000

# head myfile
"1017942629"
"1018292324"
"1023462896"
"1023463449"
"1023469166"

It makes no difference to epoch_time.awk if the values have quotes around them or not.

This may be a case of not being able to see the forest for the trees. But I am confused and can't see where the problem may lie.

Any help is appreciated.
10 REPLIES 10
Patrick Wallek
Honored Contributor

Re: AWK confusion

Hmmm....Let's try attaching that file again.....

epoch_time.awk hopefully attached this time.
Solution

Re: AWK confusion

Patrick,

its the double quotes - this works

for i in $(cat myfile | cut -d\" -f 2)
do
./epoch_time.awk ${i}
done

HTH

Duncan

I am an HPE Employee
Accept or Kudo
Peter Nikitka
Honored Contributor

Re: AWK confusion

Hi,

of course is matters, whether your values in the file are quoted or not. Try

for i in $(do
eval ./epoch_time.awk ${i}
done

as a shortcut to eliminate the quotes.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Hein van den Heuvel
Honored Contributor

Re: AWK confusion

It's the quotes. Strip them!

fwiw... I don't care for any scripts with hardcoded day tables. Yuck.

Cheers,
Hein.
Patrick Wallek
Honored Contributor

Re: AWK confusion

Well %$%#$%@$#%$#%!!!!!!!

I thought I tried stripping the quotes from the values in 'myfile'. Apparently not.

I guess what was throwing me off was that I had tried running the epoch_time.awk from the command line with the value quoted and it returned the correct value.

Live and learn.......

Thanks for pointing out what should've been fairly obvious.
A. Clay Stephenson
Acclaimed Contributor

Re: AWK confusion

... and Patrick, why in the Wide World of Sports would you call an awk script for what is a one-liner in Perl using time() and localtime() or gmtime().
If it ain't broke, I can fix that.
Patrick Wallek
Honored Contributor

Re: AWK confusion

Because I haven't found / taken / made the "time" to learn much Perl yet.
Hein van den Heuvel
Honored Contributor

Re: AWK confusion

Patrick..

On the live and learn...

May I finish that sentence with 'perl'

:-) :-)

You'll be in Vegas right?
We'll have a beer and i'll get you going on Perl :-)

perl -ne 'chomp;s/"//g;@t=localtime($_); printf ("%4d%02d%02d%02d%02d%02d\n", $t[5]+1900, $t[4]+1, $t[3], $t[2], $t[1], $t[0])' myfile

James R. Ferguson
Acclaimed Contributor

Re: AWK confusion

Hi Patrick:

A bit late I am, but even shorter with Perl:

# perl -MPOSIX -ne 'print strftime("%Y%m%d%H%M%S\n",localtime($_))' file

The nice part is that the directives to 'strftime' are as you find them in the manpages.

Regards!

...JRF...