LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2013, 05:45 AM   #1
jonbob75
LQ Newbie
 
Registered: Oct 2013
Posts: 2

Rep: Reputation: Disabled
awk and strftime/mktime - characters added to end of date/time conversion string


Hi,
I'm parsing log files using awk and changing their format to make them readable to a log file analyser. The awk command I'm using works fine, except for the date/time conversion.
The log file I'm parsing has entries like this:
Code:
2013-01-01 00:00:44 127.0.0.1 12345W@uni.edu 8hd86863gd 200 8042 http://journal.com
And I'm trying to get them in this format:
Code:
127.0.0.1 8hd86863gd 12345W@uni.edu [01/Jan/2013:00:00:44 +0000] "GET http://journal.com HTTP/1.1" 200 8042
The awk command I'm using to convert them is this:
Code:
awk 'BEGIN {FS=OFS=" "}
{format = "%d/%b/%Y:%H:%M:%S %z" split($1,date,"-") split($2,time,":")}
{datetime = (date[1] " " date[2] " " date[3] " " time[1] " " time[2] " " time[3] " 0")}
{print $3,$5,$4,"["strftime(format, mktime(datetime))"]","\"GET " $8 " HTTP/1.1\"",$6,$7}' 
oldFormat.log > newFormat.log
The newFormat.log file created is exactly as I need it, except the new date/time has "33" appended to it. So the time part looks like this:
Code:
[01/Jan/2013:00:00:44 +000033]
Any idea why this is happening? I've tried changing the formation of the datetime string, but this "33" is appended to it no matter what the format.

Thanks,
jb
 
Old 10-07-2013, 06:00 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by jonbob75 View Post
Any idea why this is happening?
Yes. The following statement
Code:
format = "%d/%b/%Y:%H:%M:%S %z" split($1,date,"-") split($2,time,":")
builds a string from three substrings. Take in mind that string concatenation in awk is performed simply by using a blank space as separator, therefore the string "%d/%b/%Y:%H:%M:%S %z" is concatenated with the return values of the two split functions, which is 3 in both cases.

Moreover, your script can be simplified by removing unnecessary stuff:
Code:
awk '{
  format = "%d/%b/%Y:%H:%M:%S %z";
  split($1,date,"-")
  split($2,time,":")
  datetime = (date[1] " " date[2] " " date[3] " " time[1] " " time[2] " " time[3] " 0")
  print $3,$5,$4,"["strftime(format, mktime(datetime))"]","\"GET " $8 " HTTP/1.1\"",$6,$7
}' oldFormat.log > newFormat.log
Hope this helps.
 
Old 10-07-2013, 06:12 AM   #3
jonbob75
LQ Newbie
 
Registered: Oct 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Yes. The following statement
Code:
format = "%d/%b/%Y:%H:%M:%S %z" split($1,date,"-") split($2,time,":")
builds a string from three substrings.
Of course! Thanks so much for spotting that!!

Quote:
Originally Posted by colucix View Post
Moreover, your script can be simplified by removing unnecessary stuff:
Code:
awk '{
  format = "%d/%b/%Y:%H:%M:%S %z";
  split($1,date,"-")
  split($2,time,":")
  datetime = (date[1] " " date[2] " " date[3] " " time[1] " " time[2] " " time[3] " 0")
  print $3,$5,$4,"["strftime(format, mktime(datetime))"]","\"GET " $8 " HTTP/1.1\"",$6,$7
}' oldFormat.log > newFormat.log
Hope this helps.
Thanks! Tried putting that in, but it threw syntax errors at the "datetime" and "print" lines until I braced them like this:

Code:
awk '{
   format = "%d/%b/%Y:%H:%M:%S %z";
   split($1,date,"-")
   split($2,time,":")}
  {datetime = (date[1] " " date[2] " " date[3] " " time[1] " " time[2] " " time[3] " 0")}
  {print $3,$5,$4,"["strftime(format, mktime(datetime))"]","\"GET " $8 " HTTP/1.1\"",$6,$7
}' oldFormat.log > newFormat.log
But everything works now anyway, so thanks a lot again!
 
Old 10-07-2013, 08:59 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You simply need semi-colons after each statement
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Non-GNU awk Splitting Single Characters In A String? brgr88 Other *NIX 1 04-02-2013 04:11 PM
[SOLVED] [bash - sed - awk] Match line with x characters and add string TigerClaw Linux - Newbie 4 02-28-2012 12:22 AM
awk from end of string -3 position comparison dazdaz Linux - Software 4 10-04-2010 10:31 AM
Date comparison with 'string date having slashes and time zone' in Bash only TariqYousaf Programming 2 10-08-2009 07:37 AM
Start Date + Time Duration = End Date/Time calculator? ToBe Linux - General 3 09-26-2005 10:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration