nginxlualua-tableopenresty

Lua NGINX String find multi lines


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?


Solution

  • You should give string.gsub([pattern], [string | table | function], [count]) a try.
    For example, to remove all newlines to return a single line choose...

    local open_file = io.open(file) -- read is the default
    local data, count = open_file:read('*a'):gsub('\n', '') -- every (returned) string has string library functions attached as method (metatable > metamethod > __index = string)
    print(('%d newline/s removed\n%s'):format(count, data))
    

    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 ""