i’m having lua program that i’m reading some files
but they are in .csv format, which I need to find some string.
When it’s having multi-lines doesn’t work, only first line is available.
Using :
local open_file = io.open(file, "r")
local data = open_file:read()
io.close(open_file)
And displaying :
local match0 = string.find(string1, data)
So it’s reading only first line in my .csv
Any suggestions?
2
Answers
file:read()
only reads a single line. To read the entire file into a string, you have to usefile:read("*a")
.Note however that you also seem to have your arguments to
string.find
swapped: The first argument is the subject string you’re searching in and the second argument is the pattern. Since you want to search in the file, you should be usingstring.find(data, string1)
.You should give string.gsub([pattern], [string | table | function], [count]) a try.
For example, to remove all newlines to return a single line choose…
Keep in Mind: string.gsub() loops over the whole string therefore the count return value will show how often the pattern
'n'
has matched and replaced with an empty string''
<– 2 single quotes or if nginx dont allow/accept single quotes you can try[[]]
or""