Last N lines of a file containing a string

I am writing an analysis to retrieve error messages from a file
e.g.
(lines of file “e:\mylog.log”) whose (it contains “FAILURE:”)
this works but I want to limit the retrieval to the last 5 lines containing FAILURE:

Any Ideas?

1 Like

I wrote this on my host file…

lines whose (line number of it > (number of lines of file "c:\windows\system32\drivers\etc\hosts" - 5)) of file "c:\windows
\system32\drivers\etc\hosts"

In your scenario it would be:
lines containing “FAILURE” whose (line number of it > (number of lines of file “e:\mylog.log” - 5)) of file “e:\mylog.log”

You could also do some if then else around it if you wanted to.
-jgo

1 Like

This will give you the last 5 lines, but not the last 5 lines containing a particular string.

oh snaps… You are correct sir. Modification in the morning ?
Can I get a sample file?

1 Like

Getting the last N lines of a file isn’t so bad, as shown by @jgo and then taking that result and filtering it to only have the lines that contain “FAILURE:” is also not so bad.

it whose(it contains "FAILURE:") of lines whose (line number of it > (number of lines of file "drivers\etc\hosts" of system x64 folder - 100)) of file "drivers\etc\hosts" of system x64 folder

The above would give you the last 100 lines of the file, but only the lines that contain “FAILURE:”, which could be adapted to point at any file instead of the hosts file.

Getting exactly 5 lines containing “FAILURE:” is much more difficult… there are a few options that might work, but they would be much slower to execute.

1 Like

You could do some tricks in action script… take the original file, filter for your search string and output to a second file. Then get the last 5 lines of the second file using the @jgo approach.

1 Like

or, you could just get jiggy with the relevance and:

q: lines whose (it as lowercase starts with “failure” and line number of it >= (((following text of last “|” of preceding text of last “|” of preceding text of last “|” of preceding text of last “|” of preceding text of last “|” of preceding text of last “|” of it) of concatenation “|” of (it as string) of line numbers of lines whose (it as lowercase starts with “failure”) of file “c:\test.txt”))as integer) of file “c:\test.txt”

2 Likes

I like this 2nd approach, but it is still a bit less than ideal, and wouldn’t scale well if you wanted the last 20 lines instead of 5. I wish there were an easier way to handle situations like this using relevance.

I think this is not actually better, other than the fact that you can specify an arbitrary number of lines to return: (in this case I’m returning the last 8 lines containing “max” instead of “failure”)

lines whose (it as lowercase contains "max" and line number of it >= ( (it as integer) of tuple string item ((it - 8) of number of lines whose (it as lowercase contains "max") of files "C:\temp\cpu-z-report.txt") of concatenation ", " of (it as string) of line numbers of lines whose (it as lowercase contains "max") of files "C:\temp\cpu-z-report.txt")) of files "C:\temp\cpu-z-report.txt"
3 Likes

Can anyone please post how to read first N lines of a file?

lines (integers in (1,10)) of file "c:\temp\something.txt"

6 Likes

relevance: (line (number of lines of it) of it) of file “C:\temp\something.txt”

2 Likes

Now in 2022 with client 10.0.4, this works:

lines (integers in ((number of lines of it - 20),(number of lines of it))) of file "foo"

4 Likes