 <p>A variation of your overall approach but with the same end result.</p> <pre><code>PATTERN = '^v (%S+) (%S+) (%S+)$' fo = io.open('object.lua','w') i = 1 for line in io.lines('file.txt') do if line:match(PATTERN) then line = line:gsub(PATTERN,'Node'..i..'= {x=%1, y=%2, z=%3}') print(line) fo:write(line,'\n') i = i + 1 end end fo:close() </code></pre>