views:

83

answers:

2

First of all, I have been using this site as a reference through the entire scripting process and it has been wonderful. I appreciate how useful and knowledgeable everyone is here. With that in mind, I have a question regarding matching (pattern matching) in Lua. I am writing a script that essentially takes input from a file and imports it into a table. I am checking for specific MAC addresses in the file as the host I am querying.

  if macFile then
     local file = io.open(macFile)

     if file then
    for line in file:lines() do
      local f = line
      i, j = string.find ( f, "%x+" )
      m = string.sub(f, i, j)
      table.insert( macTable, m )
    end
    file:close()
     end

This parses the file into a format I will use to query later. Once the table is built, I run a pattern matching sequence to try and match the MAC from the table by iterating the table and matching the pattern against the current iteration:

local output = {}
t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")
for key,value in next,macTable,nil do
        a, p = string.find ( s, value )
        matchFound = string.sub(s, a, p)
        table.insert( output, matchFound )
end

This doesn't return any output although when I enter it line by line in a Lua prompt, it seems to work. The variables are being passed correctly I believe. Any suggestions?

A: 

I am just leaving from work and can't have a deeper look at your problem right now, but the next two lines seem quite odd.

t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")

Basically you are removing all the ':' characters in the string t. Afterwards you end up with s being "000000000000". This is probably not what you want?

ponzao
Well, essentially the MAC file I am importing from is a mac, line by line, without the ":". So my table is filled with a number of different entries all formatted without the ":". So, to match the table formatting, I need to format t. In the script however, the variable t will be automatically populated. But thanks for the write back!
Timothy
Have you verified you are able to parse the file correctly? You can test it by doing `for k, v in pairs(macTable) do print(k, v) end`. Could you post a part of the file you are trying to parse?
ponzao
+1  A: 

If your macFile uses a structure like this:

012345678900
008967452301
000000000000
ffffffffffff

The following script should work:

macFile = "./macFile.txt"
macTable = {}

if macFile then
    local hFile = io.open(macFile, "r")
    if hFile then
        for line in hFile:lines() do
            local _,_, sMac = line:find("^(%x+)")
            if sMac then
                print("Mac address matched: "..sMac)
                table.insert(macTable, sMac)
            end
        end
        hFile:close()
    end
end

local output = {}
t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")

for k,v in ipairs(macTable) do
    if s == v then
        print("Matched macTable address: "..v)
        table.insert(output, v)
    end
end
Adam
Thank both of you very much. Adam, that was exactly what I was looking for. It didn't seem as easy as a simple equals condition. I appreciate your time.
Timothy