skip to Main Content

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


  1. file:read() only reads a single line. To read the entire file into a string, you have to use file: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 using string.find(data, string1).

    Login or Signup to reply.
  2. 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 removedn%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 ""

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search