Age of file content

I am trying to pull info from a log file which containing one specific text as last uploaded on with date & time, I am able to print date & time but want to compare that with current date & time to get age of that content.

if (not Windows of operating system) then (if not (exists folder "/var/opt/log/XYZ" whose (exists file "uploader.log" of it)) then "None" else (it as trimmed string) of preceding texts of first " (" of following texts of last "[" of (item 1 of it) of (item 1 of it, lines of item 0 of it ) whose (line number of item 1 of it = item 0 of it ) of (it, (maximum of (line number of it + 1 ) of lines whose (it contains "Upload successful") of it)) of file "/var/opt/log/XYZ/uploader.log") else (if not (exists folder "C:\windows\XYZ" whose (exists file "uploader.log" of it)) then "None" else (it as trimmed string) of preceding texts of first " (" of following texts of last "[" of (item 1 of it) of (item 1 of it, lines of item 0 of it ) whose (line number of item 1 of it = item 0 of it ) of (it, (maximum of (line number of it + 1 ) of lines whose (it contains "Upload successful") of it)) of file "C:\windows\XYZ\uploader.log")

Line which I am targeting -

[Sun 07 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} Upload successful

1 Like

Any suggestion to get it another way ! I just want to get the age of below latest/recent line found in that file.

Hello vk.khurava,

This doesn’t seem right:

exists folder "/var/opt/log/uploader.log"

and

exists folder "C:\windows\uploader.log"

Those are files, not folders.

Regards,
Vitaliy

Yup, pls ignore that I changed files/folder details to avoid any issue on social page.

updating original relevance.

Please try this:

q: if (not windows of operating system) then (if (exists file "/var/opt/log/uploader.log") then ((it as trimmed string) of preceding texts of first " (" of following texts of last "[" of it whose (it contains "Upload successful") of lines whose (line number of it > (number of lines of file "/var/opt/log/uploader.log" - 1)) of file "/var/opt/log/uploader.log") else ("File does not exist")) else (if (exists file "C:\windows\uploader.log") then ((it as trimmed string) of preceding texts of first " (" of following texts of last "[" of it whose (it contains "Upload successful") of lines whose (line number of it > (number of lines of file "C:\windows\uploader.log" - 1)) of file "C:\windows\uploader.log") else ("File does not exist"))

Regards,
Vitaliy

Thanks, Vitaliy !
but I am looking for age of below content, output I am already getting dont know where to put “NOW” to compare “Sun 07 Apr 2019 11:53:12 AM” with current date & time to get the age in result.

Like the recent/latest upload successful line in uploader.log file is 3 days older now.

Please note, it would be much easier if you could keep dates in the file uploader.log in a standard format:

q: ("Sun, 07 Apr 2019 11:53:12 -0500") as time - now
A: -2 days, 16:26:09

q: ("07 Apr 2019 11:53:12 -0500") as time - now
A: -2 days, 16:29:24

Or something like that…

Yes, like this way, but we cant control date time format in that log file, its hard coded in application. so if we can just get the age even on basis of date.

Probably not the best way to use substrings here, but, as you said, the date format is controlled by another app, so please try this:

q: ((substring (0,3) of it & "," & substring (4,20) of it & substring (27,4) of it) of (if (not windows of operating system) then (if (exists file "/var/opt/log/uploader.log") then ((it as trimmed string) of preceding text of first " (" of following text of last "[" of it whose (it contains "Upload successful") of lines whose (line number of it > (number of lines of file "/var/opt/log/uploader.log" - 1)) of file "/var/opt/log/uploader.log") else ("File does not exist")) else (if (exists file "C:\windows\uploader.log") then ((it as trimmed string) of preceding text of first " (" of following text of last "[" of it whose (it contains "Upload successful") of lines whose (line number of it > (number of lines of file "C:\windows\uploader.log" - 1)) of file "C:\windows\uploader.log") else ("File does not exist")))) as time - now
A: -2 days, 17:02:43

Regards,
Vitaliy

3 Likes

Thanks Vitaliy ! but didn’t worked for me, below is the file content if it provide more clarity…

[Sun 07 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} Upload directory is 'C:\windows\upload'
[Sun 07 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} Will attempt to upload file(s) 
[Sun 07 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} Upload directory is 'C:\windows\upload'
[Sun 07 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} Will attempt to upload file(s) '*.log'
[Sun 07 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} Upload directory is 'C:\windows\upload'
[Sun 07 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} Upload successful
[Sun 07 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} WARNING: Source directory 'C:\windows\upload\UsageData' is not accessible or does not exist
[Sun 07 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} WARNING: Source directory 'C:\windows\upload\UsageData' is not accessible or does not exist
[Sun 08 Apr 2019 10:53:12 AM CDT (G, 0)] {10347} Upload directory is 'C:\windows\upload'
[Sun 08 Apr 2019 10:53:12 AM CDT (G, 0)] {10347} Will attempt to upload file(s) 
[Sun 08 Apr 2019 10:53:12 AM CDT (G, 0)] {10347} Upload directory is 'C:\windows\upload'
[Sun 08 Apr 2019 10:53:12 AM CDT (G, 0)] {10347} Will attempt to upload file(s) '*.log'
[Sun 08 Apr 2019 10:53:12 AM CDT (G, 0)] {10347} Upload directory is 'C:\windows\upload'
[Sun 08 Apr 2019 10:53:12 AM CDT (G, 0)] {10347} Upload successful
[Sun 09 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} Upload directory is 'C:\windows\upload'
[Sun 09 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} Will attempt to upload file(s) '
[Sun 09 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} Upload directory is 'C:\windows\upload'
[Sun 09 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} Will attempt to upload file(s) '*.log'
[Sun 09 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} Upload directory is 'C:\windows\upload'
[Sun 09 Apr 2019 11:53:12 AM CDT (G, 0)] {10347} Upload successful
[Sun 10 Apr 2019 09:53:12 AM CDT (G, 0)] {10347} Upload directory is 'C:\windows\upload'
[Sun 10 Apr 2019 09:53:12 AM CDT (G, 0)] {10347} Will attempt to upload file(s) '
[Sun 10 Apr 2019 09:53:12 AM CDT (G, 0)] {10347} Upload directory is 'C:\windows\upload'
[Sun 10 Apr 2019 09:53:12 AM CDT (G, 0)] {10347} Will attempt to upload file(s) '*.log'

In my case I am getting below msg -

Q:((substring (0,3) of it & "," & substring (4,20) of it & substring (27,4) of it) of (if (not windows of operating system) then (if (exists file "/var/opt/log/uploader.log") then ((it as trimmed string) of preceding text of first " (" of following text of last "[" of it whose (it contains "Upload successful") of lines whose (line number of it > (number of lines of file "/var/opt/log/uploader.log" - 1)) of file "/var/opt/log/uploader.log") else ("File does not exist")) else (if (exists file "C:\windows\uploader.log") then ((it as trimmed string) of preceding text of first " (" of following text of last "[" of it whose (it contains "Upload successful") of lines whose (line number of it > (number of lines of file "C:\windows\uploader.log" - 1)) of file "C:\windows\uploader.log") else ("File does not exist")))) as time - now
E: Singular expression refers to nonexistent object

I believe that this is because you are reading the last line of the file and as that last line from your example text does not match what you are searching for, it results in the singular expression error.

Q: (lines whose (line number of it > (number of lines of file "C:\windows\uploader.log" - 1)) of file "C:\windows\uploader.log")
A: [Sun 10 Apr 2019 09:53:12 AM CDT (G, 0)] {10347} Will attempt to upload file(s) '*.log'
T: 24.910 ms
I: plural file line

Q: ((it as trimmed string) of preceding text of first " (" of following text of last "[" of it whose (it contains "Upload successful") of lines whose (line number of it > (number of lines of file "C:\WINDOWS\uploader.log" - 1)) of file "C:\WINDOWS\uploader.log")
E: Singular expression refers to nonexistent object.

Yes ! but in my original relevance we are able to read & print recent upload successful line’s time whether its last or 2nd last or any number but requirement is to call the most recent line from the log which contain upload successful & match its date & time with current date & time to get age of that line.

Any suggestions, please apologies I am not good with it.

Here’s another approach. It was an interesting exercise in relevance!

I assumed that log file entries will be recorded in the same time zone as the client (CDT in this case), and took into account whether the time stamp was morning (AM) or afternoon (PM)

if (windows of operating system) then ((((now - ((substring(4,12) of it & (if (substring(25,2) of it = "AM") then (substring(16,8) of it) else ((substring(16,2) of it as integer + 12) as string & substring(18,6) of it)) & " " & local time zone as string)) of (substring(1,31) of line (maxima of line numbers of lines whose (it contains "Upload successful") of it) of it) as time) as string) | "no line(s) found") of (file "C:\Windows\uploader.log") | "no file found") else ((((now - ((substring(4,12) of it & (if (substring(25,2) of it = "AM") then (substring(16,8) of it) else ((substring(16,2) of it as integer + 12) as string & substring(18,6) of it)) & " " & local time zone as string)) of (substring(1,31) of line (maxima of line numbers of lines whose (it contains "Upload successful") of it) of it) as time) as string) | "no line(s) found") of (file "/var/opt/log/uploader.log") | "no file found")

2 Likes

Thanks so much its worked & able to get the age but when I implemented this got to know OS wise log file entry is differ from each other if please you can help me with combined them-

Windows workstation:
[12/04/2019 00:57:12 (G, 0)] {12064} File 'XXXX at 20190412T005645_{739483E8-0D41-4206-A451-B0FF59983C98}.log' removed from upload directory
[12/04/2019 00:57:12 (G, 0)] {12064} Upload successful
[12/04/2019 00:57:12 (G, 0)] {12064} Uploading finished
[12/04/2019 00:57:12 (G, 0)] {12064} ************************************************************

Windows server:
[4/11/2019 1:00:34 PM (G, 0)] {10100} File XXXXX at 20190411T125347_{1A78FDA3-0D36-42E0-850C-2BD6CA6C8236}.log' removed from upload directory
[4/11/2019 1:00:34 PM (G, 0)] {10100} Upload successful
[4/11/2019 1:00:34 PM (G, 0)] {10100} Uploading finished
[4/11/2019 1:00:34 PM (G, 0)] {10100} ************************************************************

CentOS server:
[Thu 11 Apr 2019 12:54:28 BST (G, 0)] {8040} File 'XXXXXX at 20190411T125418_{0AD81192-FA77-477A-A127-C96DFB396E3B}.log' removed from upload directory
[Thu 11 Apr 2019 12:54:28 BST (G, 0)] {8040} Upload successful
[Thu 11 Apr 2019 12:54:28 BST (G, 0)] {8040} Uploading finished
[Thu 11 Apr 2019 12:54:28 BST (G, 0)] {8040} ************************************************************

Well now, this raises the bar a bit. Some systems use European date representations, reversing the month and day numbers and others use US date representations, without leading zeroes for the months, days, and hours.

If I had to guess, the Windows machines are using local time & date settings. If you looked at a Server in EMEA, it would probably look just like the workstation in your sample.

If I have some time this weekend, I’ll give it a go.

1 Like

I was able to account for the three use cases you presented by making a few more assumptions:

  • Use case 1 - this is the default date/time stamp for non-US Windows computers, and, so uses European date and 24 hour time representations.
  • Use case 2 - the Windows computers in the US will have a user locale of English (United States), to distinguish between use case 1 and use case 2
  • Use case 3 - this represents the Unix style of date/time representation

So we have to test for OS (Windows vs. non-Windows) and locale (US vs non-US) for Windows computers.

if (windows of operating system) then (if (user locale as string contains "United States") then ((((now -((unique value of ((day_of_month(tuple string item 1 of it) & month(tuple string item 0 of it as integer) & year(tuple string item 2 of it)) as string) of (concatenations ", " of substrings separated by "/" of tuple string item 0 of it)) & " " & ((if (tuple string item 2 of it = "PM") then (((preceding text of first ":" of tuple string item 1 of it) as integer + 12) as string) else (last 2 of ("00" & preceding text of first ":" of tuple string item 1 of it))) & ":" & following text of first ":" of tuple string item 1 of it) & " " & local time zone as string) of (following text of first "[" of (preceding text of first " " of it as trimmed string & ", " & (concatenation ", " of substrings separated by " " of (following text of first " " of (preceding text of first "(" of it as trimmed string)))) of (line (maxima of line numbers of lines whose (it contains "Upload successful") of it) of it)) as time) as string) | "no line(s) found") of (file "C:\Windows\uploader.log") | "no file found") else ((((now - ((unique value of ((day_of_month(tuple string item 0 of it) & month(tuple string item 1 of it as integer) & year(tuple string item 2 of it)) of (concatenations ", " of substrings separated by "/" of tuple string item 0 of it)) as string) & " " & tuple string item 1 of it & " " & local time zone as string) of (following text of first "[" of (preceding text of first " " of it as trimmed string & ", " & following text of first " " of (preceding text of first "(" of it as trimmed string)) of (line (maxima of line numbers of lines whose (it contains "Upload successful") of it) of it)) as time) as string) | "no line(s) found") of (file "C:\Windows\uploader.log") | "no file found")) else (((((now - ((substring(0,11) of it) & " " & (if (substring(21,2) of it = "PM") then (((substring(12,2) of it as integer) + 12) as string & substring(14,6) of it) else (substring(12,8) of it)) & " " & local time zone as string) of ((tuple string item 1 of it) of (following text of first "[" of (preceding text of first " " of it as trimmed string) & ", " & following text of first " " of (preceding text of first "(" of it as trimmed string)) of (line (maxima of line numbers of lines whose (it contains "Upload successful") of it) of it)) as time) as string) | "no line(s) found") of (file "/var/opt/log/uploader.log") | "no file found"))

1 Like

Many thanks it perfectly working for Windows but on Linux its showing no file found.

Below is the latest line from Linux log file.

[Fri 05 Apr 2019 12:25:25 PM CDT (G, 0)] {35416} Upload successful

What is the path to the file and the name of the file in Linux? Perhaps I have mistyped it…

I believe you have provided correct path /var/opt/log/uploader.log its just earlier linux line was having BST but this line havingg CDT, can this be a problem

it shouldn’t matter, no. I am only looking at the date and time in the entry, assuming that the time zone in the log is the same as the machine’s time zone.

1 Like