1

I have a variable in which stores the DateTime in awk

test=$(f["DateTime"])
print "Printing Test variable:",test 

Output:

 Printing Test variable:,"2018-12-18 18:36:55"

I want to convert the value in test to seconds

Could anybody tell me how to do that in awk

Using mktime() is giving -1 as output

    print mktime(gensub(/[ :-]/," ","g",test))

My Input data (CSV File)is like below

DateTime,Dealer,Some Value,State,CallEndTime,Some Value,TotalDuration,,..

 "2019-01-07 11:35:42","Car","fab","foo",,"bar","100","boo",..

I got the DateTime value from this,

1

If you have GNU awk (gawk) you can use its mktime function - however the input must be a space-separated datespec of the form "YYYY MM DD HH MM SS [DST]". The date fields in your variable are in the right order, but you will need to replace the delimiters with spaces:

$ gawk -v test="2018-12-18 18:36:55" 'BEGIN{print mktime(gensub(/[ :-]/," ","g",test))}'
1545176215

See for example Time Functions in the GNU Awk User's Guide


If your string is enclosed in double quotes, you will need to remove those as well:

$ gawk -v test='"2018-12-18 18:36:55"' 'BEGIN{print mktime(gensub(/[ ":-]/," ","g",test))}'
1545176215
  • I tried this,but its giving me -1 as O/P, Please check the edited question – mittu Jan 23 '19 at 4:05
  • @mittu please see update – steeldriver Jan 23 '19 at 4:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.