r/lua • u/ACleverMoose • 2h ago
Help Trouble parsing through .txt file to find lines with specific string
I'm trying to make a program to look through a .txt file and look for any line with the word MODULE in it. So far I am not having any luck.
The file is quite large, about 7.5 million lines. And I am just dipping my toes into Lua. I was able to access the file and make a line by line copy after getting random character puke by reading and writing as binary.
I stuck a print statement immediately after the for loop and that would print ok.
But when I try and get anything to occur in the IF portion it seems to do nothing.
local path = 'C:\\filepathgoeshere'
--create file path
local file = io.open(path, "rb")--open file in path
if file then --if the file is found, read and create PID file
local outputfile = io.open('CHA_PID.txt', 'w+b')
if not outputfile then --if the output file cannot be created, close input and return
file:close()
print("Error: Could not create output file CHA_PID.txt")
return
else
for line in file:lines() do
if string.find(line, 'MODULE') then
--outputfile:write(line .. "\n")
print('FOUND ONE!\n')
--outputfile:write('FOUND ONE!\n')
return
end
end
end
outputfile:close()
file:close()
return
else --if file not found, print error message and return
print('Error: Could not open file at path: ' .. path)
return
end