Skip to content

Instantly share code, notes, and snippets.

@Kasahs
Created July 19, 2018 17:16
Show Gist options
  • Save Kasahs/9e4069000c2ee7a2721760c76a89cc2d to your computer and use it in GitHub Desktop.
Save Kasahs/9e4069000c2ee7a2721760c76a89cc2d to your computer and use it in GitHub Desktop.
read lines from file in reverse order and write to stdout till match is found
import os
import sys
def readlines_reverse(filename):
with open(filename) as qfile:
qfile.seek(0, os.SEEK_END)
position = qfile.tell()
line = ''
while position >= 0:
qfile.seek(position)
next_char = qfile.read(1)
if next_char == "\n":
yield line[::-1]
line = ''
else:
line += next_char
position -= 1
yield line[::-1]
def main():
infile_path = sys.argv[1]
queries = str(sys.argv[2]).split(',')
for line in readlines_reverse(infile_path):
found = True
for query in queries:
if line.find(query) <= -1:
found = False
break
if found:
break
print(line)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment