Find in the last 100 line of txt file

hey guys,
i need help with my txt file.

i want to know if there is a specific string in the file but i want to check only in the last 100 lines in the file

Try this but you may have a problem if you have lines with a lot of characters… In my testing if a line has a LOT of characters and especially if you are using special ones then relevance replace them to ASCII representations so one characters becomes 5 (for example, instead of double-quote you see %2522) and that means that what would have been 1 line is read back by relevance as 2 lines from there the numbers are off. In my particular testing 67 lines worth of file were shown as 100 lines by relevance…

(item 1 of it) of ((if (it >= 100) then (it - 99) else (0)) of number of lines of it, lines of it) whose (line number of item 1 of it >= item 0 of it) of file “…”

yeah its looks good but i need with a specific word searching and without if instead of it want to use exists

Missed that requirement. try this:

(item 1 of it) of ((if (it >= 100) then (it - 99) else (0)) of number of lines of it, lines of it, “search string”) whose (line number of item 1 of it >= item 0 of it and item 1 of it contains item 2 of it) of file “…”

Q:(item 1 of it) of ((if (it >= 10) then (it - 9) else (0)) of number of lines of it, lines of it, “fa”) whose (line number of item 1 of it >= item 0 of it and item 1 of it contains item 2 of it) of file “D:\123.txt”
E: Singular expression refers to nonexistent object.

Are you sure this file exist? the code works for me just fine. it returns the lines when the string is matched and empty result set if no matches.

1 Like

now its work=/ i dont know what i did diffetent =/
tnx
now i need to understand how to do it with “exists”

Cool! Stick exists in front and it should work:

exists ((item 1 of it) of ((if (it >= 100) then (it - 99) else (0)) of number of lines of it, lines of it, “search string”) whose (line number of item 1 of it >= item 0 of it and item 1 of it contains item 2 of it) of file “…”)

Actually, there is something might have caused the above issue. Relevance inspectors can’t read files that other programs have locked… That’s why you can’t read the current BF agent log file for example but you can any of the ones from previous days, so if you had it open with an editor that is locking the files it may have been causing the inspector to fail, although I would’ve thought it would have produced a bit more user-friendly error message…

More recent agents support the ‘locked lines’ inspector to overcome some of the issues with files locked open

3 Likes

trying to do with out the if statement

This is how to get the last 100 lines of a txt file in relevance but only reading the lines a single time ( more efficient )

(item 1 of /* -> This "it" refers to the last 100 lines of the file -> */ it) whose( /* -> remove empty lines, which is why this relevance can return less than 100 lines per file -> */ it as trimmed string != "") of ( /* -> this is the number of lines of the file from the previous statement -> */ item 1 of it, (lines of /* -> the file object -> */ item 0 of it) ) /* -> This whose statement is responsible for filtering for only the last 100 lines of the file -> */ whose ( (line number of /* -> lines of the file -> */ item 1 of it) > ( /* -> number of lines of the file -> */ item 0 of it - 100 /* <- This is the number of lines to return, which is subtracted from the total # of lines <- */ ) ) of ( /* -> the parent file object itself -> */ it, number of lines of it) of files "whatever.log"

See here: https://bigfix.me/relevance/details/3002375

This relevance was written before the newer “locked lines” inspector which might be better to use instead of just “lines”.

I would recommend testing relevance like this with an extremely large file as a test to make sure it is efficient.

2 Likes

but its not searching a word in the lasts N lines=/