0

I am writing a section of awk code that reads a log file of this format:

[03/02/2020 10:01:01] SOME DATA [03/02/2020 10:05:25] SOME MORE DATA [03/02/2020 11:54:38] AND YET SOME DATA 

etc.

I wish to determine the epoch timestamp by combining the data in fields $1 and $2 and including a space between them to satisfy the input format needed by the bash date command.

This is the section of code I have written:

cmd="date -d" substr($1,2,10) substr($2,1,8) " +%s" cmd | getline this_date print this_date 

At the moment, when i run the script, i get this error...

date: invalid date ‘03/02/202010:01:01’ date: invalid date ‘03/02/202010:05:25’ date: invalid date ‘03/02/202011:54:38’ 

etc.

So it seems to me I am nearly there but, as expected, the date command wants a space between the date and time portions.

I have tried many ways to try and code a 'space' between substr($1,2,10) substr($2,1,8) in the first line of code above but each time I get an error.

Can anyone advise the best approach?

1
  • Do you have or can you get GNU awk? It has built in time functions that'd make this task easier and more efficient and since you're using GNU date for -d I suspect you do also have GNU awk (run awk --version if you're not sure). Which timezone are the dates in your log file? Which timezone will you be running the tool from? Commented Feb 7, 2020 at 16:17

1 Answer 1

2

You are lacking a space between the two substrs you extract; and you need to add quotes to join them into a single string.

cmd="date -d \"" substr($1,2,10) " " substr($2,1,8) "\" +%s" 

If you can use single quotes instead of double, that will simplify things a bit; but Awk scripts are often passed between single quotes, so I assumed you'd prefer double quotes here.

Just to spell this out, the command being run is

date -d "03/02/2020 10:01:01" +%s 

which gets turned into

date -d 03/02/2020 10:01:01 +%s 

once the shell is done parsing it. The quotes are necessary to preserve the argument to the -d option as a single string even though it contains a space. As such, this is more of a shell question than an Awk question really.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm guessing this is Apache log format; most sane programs produce computer-readable time stamps in their logs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.