How to find N lines containing a specific string given an offset reversely? With python on Unix.
Given a file:
a
babc1
c
abc1
abc2
d
e
f
Given the offset: 20 (that's "d"), the string: "abc", N: 2, the output should be:
strings:
# the babc1 will not count since we only need 2
abc1
abc2
offset: (we need to return the offset where the search ends)
10 ((the "a" in "abc1")
The above example is just a demo, the real file is a 33G log, that why I need to take offset as input and output.
I think what the core problem is that: how to reversely read lines from a file with a given offset? The offset is near the tail.
I tried to do it with bash, it was agony. Is there an elegant, efficient way to do it in python2? Besides we will run the script with suitable( an capsule of ansible), so the dependency should be as simple as possible.