Extracting a date from a logfile

I have a log file that contains information in this format:

Thu Jun 30 20:30:54 AKDT 2011
round-trip min/avg/max/stddev = 551.371/564.656/576.922/10.456 ms
Thu Jun 30 20:35:54 AKDT 2011
round-trip min/avg/max/stddev = 545.679/578.909/633.677/39.020 ms

I want to extract the date from the first line…is there an easy way to do this in Applescript?



Hi Mike,

Maybe code as follows gives you a good starting point:

set filepath to POSIX file "/Users/martin/Desktop/test.txt"
set filecontents to read (filepath as alias)

set paras to paragraphs of filecontents
set founddates to {}

repeat with para in paras
	if para does not start with "round-trip" then
		set {dayname, monthname, daynum, hournum, minutenum, secondnum, mode, yearnum} to words of para
		set monthnum to my getmonthnum(monthname)
		set datestr to (daynum & "." & monthnum & "." & yearnum & " " & hournum & ":" & minutenum & ":" & secondnum) as text
		set founddates to founddates & (date datestr)
	end if
end repeat

repeat with founddate in founddates
	display dialog (founddate as text)
end repeat

on getmonthnum(monthname)
	set monthnum to missing value
	set monthnames to {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Dez"}
	if monthname is in monthnames then
		repeat with i from 1 to 12
			if (item i of monthnames) is equal to monthname then
				set monthnum to i
				exit repeat
			end if
		end repeat
	end if
	return monthnum
end getmonthnum

Best regards from Berlin,


If it’s literally the date in the first line that’s needed and the text in the file consists of 8-bit characters, the process could simply be:

set logPath to "path:to:logfile" -- Your HFS path here.

set {dummy, m, d, hr, min, sec, dummy, y} to words 1 thru 8 of (read file logPath)
set m to (offset of m in "**JanFebMarAprMayJunJulAugSepOctNovDec") div 3
tell (current date) to set {day, year, its month, day, time, theDate} to {1, y, m, d, hr * hours + min * minutes + sec, it}

return theDate